Welcome back.
And this video we're going to cover abstract classes and abstract classes are a little bit similar to
interfaces, but they are still different, so let's look at them.
An abstract class cannot be instantiated.
That's the same for interfaces.
You can create an object of an interface.
However, you can inherit subclasses from an abstract class and the members.
So properties and the methods of an abstract class are not abstract unless you explicitly use the abstract
key word to make them abstract.
So let's have a look at an example.
So I have this abstract class memo here, which has a constructor here, a primary constructor with
a name and origin and the weight.
And these are concrete, non abstract properties.
All right.
So the three that we created here name origin and weight.
They are non abstract.
But this one here is so I create a property called Nextbit, which is abstract, so it must be overridden
by its subclass.
So if the class inherits from Memmo, then it will need to implement those.
So it needs to override those.
The same goes for these methods.
You can see we can create methods without a body.
So we just add the abstract key word abstract fun and make run an abstract function.
The same goes for a brief here, and then we can also create concrete so abstract methods in such an
abstract class.
So we're not limited to abstract methods.
We can also just create a method such as display details that we have here, which will just display
the name origin, weight and speed of the mantle that we're creating.
So now let's look at this class that inherits from mammal.
It has also the relation to weight and the max speed.
It has also the name the region, the weight, but it also overrides the max speed.
So this abstract property that we created here.
So now our human class will overwrite that.
Then we also override the run function, so we implement the run function with our own code for all
human.
We do the same for breath.
And then we have another class called Elephant, which does the same thing, so it overrides the next
beat and is implements its own code for run and it's uncalled for breath.
Of course, they could both have a bunch of more functions and a bunch more properties.
But I'm going to keep it super simple in this example.
All right.
And now we can go to our function May, where we create an object of human, which is called Dennis,
was born in Russia as a weight of 70 kilos and a speed of 28 kilometers per hour, even though I'm not
sure whether that is true.
So then we have the elephant, which is called Rosie.
It's from India that weighs 5400 kilos, and it has a speed of 25 kilometers per hour.
So as it seems, I could outrun Rosie, but depends on how long I can do that.
So then if we call Human Run, it's going to execute the run functionality for the human, says runs
on two legs.
And if we called the same for the elephant, it will say runs on four legs.
So let's just test it.
By the way, we can also see that we are breathing so the human caused breathing and the elephant calls
breathe.
So there we are.
We see it says runs on two legs, runs on four legs.
Breathe through mouth or nose.
Breathe through the trunk.
So I'm not saying breaths here because it just needs to breathe.
So I force it to breathe.
All right.
Chromatically.
Still correct?
All right.
And that is already the major part about abstract.
And let's have a try to create a mammal here.
Memo, and I'm going to say, Dennis, it's a memo, so here then, is Russia.
Then weight of seventy point seven and finally a speed of 28.0, so trying to do that, you can see,
cannot create an instance of an abstract class, so you cannot do that.
You can, however, of course do that with classes that inherited from it.
So our human and our elephant, which are both classes that inherit from our abstract class called mammal.
All right.
So what is the difference between an abstract class and an interface?
Because you might say, all right, there must be pretty much the same right or they're very similar
to each other.
Well, the thing is that an interface cannot hold state and we can implement multiple interfaces, but
only one class.
So when implementing an interface, as we have done in the last example here where we implemented driver
pool, we could implement multiple interfaces here.
We're not limited to just one interface, but we can only inherit from one class.
Also, classes have constructors, so even an abstract class has a constructor.
You can see that our interface doesn't have a constructor here.
And the faces cannot hold fields, so basically abstract classes can have everything that interfaces
can add.
Additionally, they can have fields and constructors.
Therefore, we can properly hold state and abstract class.
All right.
So in the next video, we're going to check out typecasting.
So see you there.