In this video, we're gonna start talking about object-orientated programming, and
in particular, about classes which is a fundamental component of
object-orientated programming in Java, and actually, other languages for that matter.
So firstly, we need to understand what objects are.
They really are the key to understanding this object-orientated terminology and
technology, as such.
So what I'd like you to do is just have a look around in the room or
the area that you are sitting at right now.
And if you do that,
you'll find that there's many examples of real world objects.
For example, I'm sitting here and I can see a computer.
I can see a keyboard.
I can see a microphone.
I can see shelves on the wall.
I can see a door.
All of these are examples of real world objects.
I'm sure if you look around wherever you are,
you can see a number of those as well.
Now, real world objects have two major characteristics, the state and behavior.
So state, in terms of a computer, might be the amount of RAM it has,
the operating system it's running, the hard drive size, the size of the monitor.
Those types of things.
An ant for that matter has got status as well.
For an ant the state might be the age, the number of legs,
the conscious state, whether the ant is sleeping or is awake.
But looking at behavior, behavior for a computer might be booting up,
shutting down, not beeping or outputting some form of sound, printing something,
drawing something on the screen, and so on.
All of those are behaviors for a computer.
For an ant, behavior might be eating, drinking, fighting,
carrying food, those types of things.
Now, software objects are a fundamental part of object-oriented programming.
They've very similar to real world objects because they also consist of state and
behaviors.
Now, a software object stores its state in fields, and we know fields as variables.
And they expose their behavior with methods, which we've talked about before.
So that's what an object is in object-orientated programming terms.
So, where does a class fit in?
Well, think of a class as a template or a blueprint for creating objects.
So as it turns out, you've been using classes all along.
And I'm sure that you've seen the keywords public and
class in the code we've completed today.
And you can actually see it on the screen right now public class Main.
So, what that is,
that's actually a statement which creates a new class in Java.
So a question that might come up at this point is why do we need to bother about
classes?
Or what benefit do classes give us in our everyday programming?
Well, think back to the basic data types that we've worked on, the primitive data
types that we've explored so far int, short and those types of things.
They're all basic data types.
They are fairly limiting.
There's only so much you can do with them.
So, a case could be made here where class could be thought of as a powerful
user-defined data type, sort of an extra data type.
That's not really correct in the true meaning, but it gives you an idea of what
classes really enable you to have a pretty powerful user-defined type, where you
can create something way more powerful than a regular primitive-type like you've
been dealing with in the past, a regular data type on steroids, if you will.
So, to take this a little bit further, let's create our first real class.
As you can see on the screen, every time we create a new project, it actually
creates a new class for us, and it calls the class Main, the capital M.
It also create that method, the lower case m for the.
That's created automatically by when we create a new project, but
let's actually go ahead and create a brand-new class ourselves.
So to do that, we need to make sure the project area is open.
We click on here to open that up, click on down where it's got SRC for source.
We wanna select where it's got our package nine.
Click on that, right-click.
Select New, select Java Class.
And we're going to create a class with the name of Car.
C-A-R making sure the first letter is capitalized, as is recommended.
When you're creating classes in Java,
the first letter should be an upper case letter.
But I will be talking more about naming conventions for class and variables,
actually, in a later video in this course.
So, first thing what I want you to notice is the package.
You've seen that before, the top.
But also check out this line, this public class car.
So, that's actually the statement to create a new class with the name Car.
Now incidentally, if you wanna see what's happening behind the scenes,
you can come over here and you can right-click, Car.
And on my Mac, I can click on Reveal In Finder.
If you're on a Windows machine,
I think the equivalent command is Show In Explorer.
When you click on it and do that what it does, it shows you the directory.
It also shows you the files that are in that directory.
And as you can see here, Intelliger has automatically created a file called
car.java, and it's in the folder Tim Buchalka.
If I actually open that out,
the top there you can see classes is the name of the project that I assigned.
Then there's an SRC folder which is for Source, and looking on the left-hand
side you can see the project view, you can see that showing you the SRC as well.
Then, it's created another folder called com.
Then another subfolder of com called timbuchalka.
And that's how packages work in Java.
So, you can see we've got a folder.
Whatever you type with a dot,
that's a sub-folder of that name is automatically created by IntelliJ.
So what's happened here is that IntelliJ has created this file, called it java.
And that is a Java Source Code file.
And every time you create a new class,
this is what's happening behind the scenes.
It's creating that class in a particular folder.
Now, we can open that up and have a look at it.
If I go into the Mac text editor,
we can see it mirrors exactly what we've got in IntelliJ.
So it's always a good idea to know what's happening behind the scenes when something
is created in IntelliJ.
Invariably, when we're dealing with a file,
it's creating a file in that particular folder.
All right, so moving on.
So public and class, as well as package for that matter, are all Java key words.
Now, the public keyword is an access modifier that we use here to
determine what access we want to allow others to have to this new class
that we're creating.
So the word public means unrestricted access to the class.
Now usually, at least initially anyway, you're gonna be using public for
pretty well all your classes.
But other access modifiers we could use include private, and
that's where no other class can access that class.
Protected, which allows classes in this package to access your class,
and you can also remove the access modifier completely if you prefer.
But we'll be talking more about what these access modifiers are in future videos.
For now, generally speaking, just go with the word public knowing that,
that needs that other classes that we create have got access to this class
without being restricted.
So that's our class defined.
But at the moment, it's not very useful because it doesn't actually do anything,
literally we've created a shell.
It's like creating a new blueprint, so remembering
that a class is a blueprint for an object that we're gonna be creating.
And in this case, it's like virtually starting out on a brand new blueprint, and
we haven't done anything yet.
We've just literally, if we were creating a blue print for houses, this is page one.
We've called the blueprint the name Car, and
we haven't done anything else at this stage.
So what we need to do is create some variables that are part of this class.
Now to date, you've seen variables used inside a method.
And they were called local variables, because they are local to or
actually belong to that method.
And you can not access those local variables outside of that method.
And you saw that also with code blocks that we created in past videos.
Once you access that, you can not access those variables outside of that block.
Now, classes allow us to create variables that can be seen and
are accessible from anywhere within the class that we're creating.
Now, these are known as class or member variables, but
most commonly refer to them as fields.
Now, when you're creating a field for a class,
you need to also specify an excess modifier that works the same way as
the excess modifier for a class definition did.
Now as a general rule, when you're defining fields in Java in a class, you go
with the access modifier private, unlike the class where we've gone with public.
So what private means when talking about fields is, well,
what we're really doing with it is we're adhering to encapsulation
which is a key fundamental rule of object-orientated programming.
So encapsulation in Java is used to hide the fields and methods,
which we'll see later, from access publicly.
So, it really means that the internal representation of an object is
gonna be hidden from view outside the object's definition.
So in other words, the internal workings of a particular object, and again,
an object is going to be created from our template from our class.
So what we're doing is we're only allowing the internals to be
accessed by this class or by this object.
We're not allowing outside access to that.
Now, this will make more sense as the video progresses.
So, what we're gonna do first is we're gonna create some fields for our class.
So let's get started and do that.
So, what we need to do is type, just going into the curly braces,
within the car class.
So we need to type private int doors.
So notice how I've typed the word private.
In the past when we've created variables, we haven't needed to do that we've
actually typed the data top and the name of the variable.
In this case, we also need to add that access modifier.
So generally, you'll always go with private, and
I'll explain more as we go through.
So these variables are actually the state component for a Car.
So these are characteristics of a Car if you will.
So we're gonna have doors, how many doors it's got.
And, let's go with say the number of wheels.
We'll int as well.
And let's go with the model.
And we're gonna go with a string there, so
we can just top freeform ticks of what the model is.
And let's say, we do the same with engine.
Engine, and let's finish off with color.
So what we're trying to create here is a template of the variables that would be
part and parcel of a Car.
Obviously, if we're creating a real application, there'd be a lot ore fields.
There could literally be hundreds of different fields that we could add
for a Car.
We're just keeping it simplistic to start with.
So this is the state component when we talked about state and behavior.
So this is the state of the Car, if you will, which we're defining as fields.
So what we've done there is we created a very basic class and
we've defined five fields.
So the next thing we need to do is we need to create an object from this class,
cuz if you recall, the class is just the template.
But we need to create an object which will sort of take that blueprint,
that definition that we've defined in the class and
create an object that we can start using.
So, to do that, what we need to do is you have to go back to the main class, and,
of course, the main, and of course, the main class was created automatically by in
IntelliJ when we created the new project.
Gonna double click into that over here on the left.
And what we can do in here is we can actually build an object and
base it on that Car.
So what we now need to do is create that object.
So, how do we do that?
Well, what we do is, well, I'll delete this code for starters.
We're in a method now, so we're gonna type a local variable,
that's local to the main method as you can see in here, and we type Car.
And notice when I type car, it's come up with com.timbuchalka [INAUDIBLE].
But this Car has been defined over here.
So as far its driver is concerned, there is now a data top.
It's actually class, but we can treat it as a data type.
So we can type Car.
Let's start, we had our first object, and let's create a car that I've always loved,
which is a Porsche.
Porsche, so this is the keyword.
This is how we define an object of Car, we call it Porsche.
Now the other thing you need to do is when you're creating a new class,
you need to initialize it.
So to do that, instead of just leaving it like that,
we need to talk equals, view and Car.
And I'll press Enter there.
Now notice that the two brackets will automatically added by IntelliJ and
put a semicolon there.
So, what we've done there is we've created a new data type called Porsche which is
a type of car.
So in other words, we've created an object called Porsche based on the template, Car.
So that's one individual object that we can work on.
We can also create another one, we can do, Car, Holden.
Holden is an Australian brand car, so we can do the same, equals new car.
So we've now got two separate objects that we've created from the template,
from the main class called Car.
Now the thing is, we can't do much with those as yet.
So for example, how do we go about accessing the state variables?
They're the fields, in other words.
Now just by that definition, what's happened behind the scenes is the object
has been created, but there's also extra functionality that Java has added to these
class, to these objects automatically.
And to see that in action, we can type Porsche, Porsche.
If you type dot, now these, all these things here are actually variables or
method, that are accessible from the class, from this object.
Now you might be thinking, what is equals hash code to string, etc.
These are added automatically by Java whenever we create a new object.
So you could say that they're inherited from the Java functionality
when we created the Car.
So, to be correct,
what's happened is the Car class has inherited these from a base Java class.
But we'll talk about some class in those top of the things later.
It's probably not the time to do that now cuz we're starting with the basics.
The bottom line here is Java has added all this code to the functionality
automatically.
And you're also free to add extra things,
which we'll be doing shortly as we move forward, anyway.
My point is at the moment though, if you look at the list of things that
are available, you can see that these equals class, and so forth.
Let's just scroll down and have a look the different keywords and so forth, but
we don't see anywhere a model, number of wheels, and so forth.
It's not showing up there.
So, how do we access those?
If you go back and check out the Car class again, remember we defined these private.
And what private means is don't allow a class
outside of the Car class to access these variables.
If you wanted those to be accessible outside of the Car Java class,
we could do that by changing this access modifier to say public.
So I'll just change the one, I'll just change the model.
If you go back and check this out now.
And now if we go back and put a dot in there,
you can suddenly see that the first thing that appears now is model.
So suddenly, model has actually appeared.
At that point, we could type porsche.model = The Carrera,
I hope I spelled that right.
So what we have defined is for our object Porsche, we're told that the model,
make and model is the field which is the state that you like for the car,
porsche.model = Carrera.
So we define the type.
So that's one way accessing the data, but it's not actually a good one.
Because it really more or less finalized the rules of encapsulation, or
what we should be doing is making sure that to know that that functionality is
restricted and placed in the code Java class.
What do I mean by that?
Well, let's go back and I'll just show you what I mean.
So, we'll go back to Car Java.
We'll change that back to private again.
What we'll do is we'll create a method now.
So, we're going to create a method.
And when you're in a class, when you create a method,
that is then attached to all the objects that you create for that particular class.
Now in the past, the only methods that we've created are static classes, and
static classes aren't really the same thing.
But we'll be talking more about those later.
For now, let's create a public method.
This video is going to be a bit long, so I'm going to pause here, and
we'll come back in part two and continue with our discussion of classes.