Welcome back and this video, we are going to look into classes, so let's go ahead and create our own
class and therefore we can have a quick look at classes that we have seen already.
And we've seen this class called Main Activity, which has this cold app compared activity here, which
just means that this is going to be the class that our class is inheriting from.
So this is a concept called inheritance, which we will look at later on.
But basically what we see is that we have this class keyword and then we have a class name which starts
with a capital letter.
So let's go ahead and create our own class and I'm going to call this class person.
So this by itself is valid code already.
So we have this class person.
And of course, this class doesn't have any properties.
It doesn't have any methods, none of that.
But this is a world class.
Now, of course, we can extend that.
We can go ahead and say that it should have a constructor.
So a constructor allows us to add values to our objects when we create an object of our class.
And these values could be something like the name.
And it's just start with the first name here and the last name.
So we could just go ahead and have these two variables here has the value built of the constructor.
So these are the parameters of the constructor, so to say.
And then in curly brackets, that's where the class body starts or where it is.
So here it starts and that's where it ends.
So in here we have our class body and we can do all of the defining of our blueprint.
So here we can go ahead and create all of the different properties and methods and those kind of things.
So now this constructor here, as you've seen here, it wasn't even there, so there's no constructor
here, and that is because it is not mandatory if we don't want to define the constructor specifically.
So if we don't want to go ahead and make this one private, for example, or if we don't want to make
this public or whatever, so we can just get rid of the constructor and make this code even easier to
read.
So we have class person and then and then we have these two values that need to be passed once we create
an object of our class person.
So how do we create an object of a class or how do we create an instance of a class?
That's another way of saying it.
Therefore, we just go ahead and use the name of our class.
So in our case person and then in brackets, that's where we want to go ahead and.
Add the two values here so you can see first name and last name, so I could do something like Dennis
1.0.
All right, that's me.
So I have this object of class person which contains those two values which are first name and last
name.
So if I now go ahead and create a variable called Dennis, it's going to be a variable of a type called
person.
So I could, of course, state that specifically here.
I could say Dennis is going to be of type person.
But as you know, Carlin does the job for us because it understands, OK, if I create an object here
after the equals sign of a specific class, then it will be of that class, so of that type, so to
speak.
So by creating a class, we created our own type.
All right.
Such as we have seen in the past already.
So we used a bunch of different types and now we are using our own type.
So instead of using INTs and strings and those kind of things, we use our own type, which is called
person.
Whenever we create a class, we can also have an in it there.
So this is the initialism, which is called once an object is created.
So as soon as we run this code, it will automatically run this initialism.
So we can just go ahead and write something like a person created and we run this code.
We will say that person created is printed to our console.
All right, so this is the initialism and you can use it in order to initialize your object.
Which pretty much means prepare your object.
Another way of saying person created would be initialized a new person object.
So that's another way what we could or another thing that we could state here.
So what we can also say is what person would create it.
So, for example, that we create a person with a specific first and last name.
So I'm just going to paste a code in here.
So we say here, initialize the new person with first name, then we use the first name of our person
and last name.
Then we used to last name of our person.
So you can see it's like us creating variables here when we use them in our constructor up there.
All right, so if you run that now, we should see that the person that we created is Dennis Pioneertown.
So let me run it again, initialize the new person, object with first name doneness and last name Pontotoc,
if you want to define default values for your first and last name, for example, you can do so as well.
So, for example, I'm going to use John Doe here as default values.
So John Doe.
And now if I go ahead and create a new object and I'm just going to call this one John, then this will
automatically be John Doe.
This person, even though we didn't specifically say that this person should be called John Doe when
creating this object.
OK, so when getting the instance of this class person and here this is because we define those default
values.
So if we run this, we can see.
Initialize the new person object with first name John Hostname, though.
Let's say we only have the last name, so I'm just going to call this person, John Peterson is going
to be a new person.
And here I just say that I want to define the last name as being Peterson.
So if we're on this now.
Then we will have three people that were created and were free instances of our class that were created,
and the last one will have the last name Peterson and the first name John, so that we are initialized
a new person object with a first name and last name, Peterson.
All right.
So this is just one example of how to create classes.
There are multiple different ones that you can use.
And in the next video, we are going to see how properties work and also how methods work.
So how we can create functions within classes.
So see you there.