Welcome back and this video, we are going to cover extra 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 cannot create an object of an interface.
However, you can inherit subclasses from an abstract class and the members.
So properties and 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 here.
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 mixed speech, which is abstract.
So it must be overridden by its subclass.
So if a class inherits from Memmel, then it will need to implement those.
So it needs to overwrite those.
The same goes for these methods.
You can see we can create methods without a body.
So we just add the abstract keyword abstract fun and make run an abstract function.
The same goes for I breathe here and then we can also create concrete, so abstract methods in such
an abstract class.
So we are 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 metal that we are creating.
So now let's look at this class that inherits from Memmel.
It has also the revision, the weight and the speed.
It has also the name, the origin, the weight, but it also overrides the next speed.
So this abstract property that we created here, so now our human class will override that.
Then we also overwrite the run function, so we implement the run function with our own code for our
human, we do the same for brith.
And then we have another class called Elephant, which does the same thing, so it overrides the speed
and is implement its own code for run and its own code for breathe.
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 main 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 twenty eight 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 five thousand four 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 cells on two
legs.
And if we call the same for the elephant, it will say runs on four legs.
So let's just test that.
By the way, we can also see that we are breathing so the human cost and the elephant can breathe.
So there we are.
We say 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 is a memo, so here then is Russia.
Then weight of seventy point seven and finally high speed of twenty eight or zero, 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 Memmel.
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 are 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 DriveAble,
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 interfaces 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.