Welcome back.
Now it's time for a super interesting topic.
And that is going to be inheritance.
And we're going to start simple and we're going to get more complex along the way.
We have seen inheritance two degree already when it comes to exceptions.
So we saw how exceptions worked and that there were different types of exceptions that all inherited
from another exception.
That is the base class exception.
And we're going to see how to use that for ourselves.
And this is part two of object oriented programming.
So part one was just to understand how classes work and how encapsulation works.
And now we're going to look at how inheritance works okay.
So let's go ahead and create a new project.
Or at least I'm going to do that.
And I'm going to call this one inheritance app okay.
So inheritance similar to what you would have when inheriting traits from your parents for example.
So you maybe have the hair color or the eye color of one of your parents.
At least the chances are super high that that's the case.
And by the way, it's like 4:00 in the morning.
Usually I don't wake up that early, so I'm going to have a cup of tea with you guys.
So let's get started.
So what do we have here?
We have our class program and what I want to show you.
And I'm going to show you that in the same namespace, in the same file okay.
So that it's really, really easy to understand.
So let's say we have this base class or this class called animal okay.
An animal can have all kinds of different traits.
And these traits would be properties.
And then it has skills.
And these skills could be the methods.
Right.
We've seen that before.
But now let's look at it from an example where we're going to inherit from our class animal with a class
let's say dog okay.
So I'm going to have a public void eat method in here because every animal is supposed to be capable
of eating.
So here it's going to say eating dot dot dot.
And then I'm going to have another class.
And this could be in a separate file.
It doesn't have to be in the same file as the animal class, but it's just easier to demonstrate it
in one go.
So then we have another class called dog which inherits from animal.
And this class will have a method called bark.
Okay, because not every animal is barking or has the capability of barking.
Some do, and mainly it's dogs.
Okay.
Cats for example, would be meowing, let's say.
So here I'm just going to say something like barking dot dot dot.
So now what is going on here?
Well the thing is here we have our base class called animal.
And it's also often called parent class superclass which is basically the class whose members are inherited.
What does it mean that the members are inherited?
Well we saw a few members before, right?
We saw properties as members.
We saw methods as members.
We saw other components as members as well.
But basically these are the traits and capabilities and so forth that we're going to inherit to our
dog.
So our dog now inherits from animal, which means that it has all of the capabilities that an animal
has.
Plus it has its own capabilities.
So by capabilities I mean properties and methods.
And well basically the members.
Right.
So in order to inherit you are using the colon sign.
And after that you use the class name of the class that you want to inherit from.
So in this case I want my dog class to inherit from animal.
We've seen that with exceptions.
So when we had the let's actually create this real quick.
So here division by or divide by zero exception.
If we look into that we saw that divide by zero exception inherits from arithmetic exception which in
turn inherits from system exception, which in turn inherits from exception.
There are multiple possible layers of inheritance, and an exception is the absolute base class.
So it's the absolute fundamentals of an exception.
But then the system exception, for example, has some specific traits that are specific to itself that
are not available inside of the base exception.
So in our example, if we take that back to our dog, an animal our dog is inheriting from animal which
allows it to eat.
But.
Our animal doesn't know about the dog in the sense that it can't bark, so not every animal can bark.
Only the dog, for example, can bark because we specifically designed it.
Now we could go ahead and have another layer where we have the different breeds of dogs, for example.
And each of them would have again new traits.
Okay.
So new levels of activity, for example, as a collie is border Collie is very active in comparison
to a greyhound where you would expect that it's very active because it's so fast, but they're rather
chill, right.
So now let's see this in action okay.
Let's see this very interesting situation in action.
By the way, I also need to add that this is the derived class slash child class or subclass.
So you see there are many different ways to describe it.
So this is the class that inherits the members of the base class.
So the dog class is the derived class or the child class of the animal class or the subclass of the
animal class.
I think you get it right, but it's just that these words, they are used interchangeably and sometimes
it's a little confusing.
But yeah, just know that there are three different ways of saying any of these types, of three different
ways of saying base class and three different ways of saying derived class.
So now let's go ahead and here make sure that our console stays awake.
And then I'm going to create a dog here okay dog my dog.
And I'm going to now use my dog's methods.
So you see that I automatically get a few options here in terms of methods.
Now all of them we created ourselves okay.
So the ones that we created is bark and eat.
So let's look at those and I will go over the others in a bit as well.
But I just wanted to go over this first.
So we see that a dog can in fact bark and a dog can also eat.
So even though we never implemented the eat method inside of our dog, it can access its parents eat
method.
So you see, the IDE is not complaining.
Let's quickly run this and you will see that our dog is barking, barking and eating.
Hopefully not at the same time.
But yeah, that's pretty much the very basic concept of inheritance, where one class inherits the members
of another class, which generates all kinds of advantages.
Now, as a coder, where you don't have to create classes that are super similar to each other, but
you can just inherit from a class that you want to inherit from that has most of the skills that you
want to use without having to modify the existing class.
That's the core point here.
We are not modifying the animal class.
If we want to add new features to our dog class, we just add that to the dog itself.
So that's pretty cool.
And that's it for this video.
It's long enough already.
See you in the next one.