8.1 Intro to Inheritance
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
4 hours 19 minutes
Hi and welcome back to the course. I hope you guys are doing well. And if you remember in the last model we went over the old P component or concept of abstraction. And in this model, we're gonna be discussing the third component of old P called inheritance.
Now, as always, I've listed out some learning objectives for you to follow throughout this entire video.
First, we're gonna be going over the high level idea of inheritance and what it entails.
Then we will be discussing its use in real world programs. And finally we will go into looking an example of inheritance.
So what is inheritance to begin now if we start with the formal definition? Inheritance is a mechanism in Java through which a class acquires of properties and those properties consists of methods and fields of another class.
The main idea of inheritance is simply to give a programmer the ability to use code in another class that they can see themselves using in their newly created class. Now, this will make more sense once we actually get into the example and into our lab following this video lecture.
So now an object or into programming there's this concept of a is our relationship, and this concept can be summed up in one simple sentence or a few simple words. You have to think of it as, for example, here,
a so an item a or or not an object. But something called a is a B type of thing. Now that makes sense. If we relate back to something like apples and fruits or a car in a vehicle. An apple is a type
of fruit, and a car is a type
of vehicle. Now, the reason that this is ah um is a or is ah, relationship is important is because that is what inheritance is that that is what inheritance is promoting, and this relationship is based on inheritance.
Now the thing with the ISA relationship is that it is you need direction. That means it only goes one way there. It doesn't go both ways. So, for example, a house is a building. But a building is not
a house. And this this I'm talking in terms of java, a language of Java terminology.
Now, here we have this quick picture that demonstrates this example further. So, as you can see, we have a class called Khar and Maruti Suzuki is a type of car, so it is a card
and it has a engine. But Marty is not an engine, and an engine is not Marty. And at the same time you cannot reverse this relationship. You have to understand that
a so a here is a B type of thing, just like Marty is a type of car and car is not a type of Marty. So it is. You need direction, as I mentioned a few seconds ago.
All right, so let's look at an example of inheritance. So here I have a bicycle class and this example was taken from the Oracle job documentation, which I will provide links to in the supplementary material.
So here in the public costs bicycle, we can see we have four methods on three fields and one constructor,
and now it's in. We want to create a sub cost of the spice to a class, and let's call it a mountain bike. Now a mountain bike is a type of bicycle. Therefore, extending it would make perfect sense because instead of writing these entire all these methods, all of these four methods all over again.
We can just inherit these methods and these properties over here
for our mountain by class, since a mountain bike is a type of bicycle
and that is the general idea of what inheritance promotes now, since both of these since a mountain bike is a bicycle, it is allowed to take and inherit all of the properties and methods of the bicycle class. And that is essentially what this example is trying to tell you.
All right, so in this video, we discussed what inheritance is
and then it's used in the real world programs is just thio elaborate to the second bullet point over here. Um, the reason someone would want to use inheritance is because you would want to, For example, one of the biggest things with inheritance is that it decreases redundancy. So all of those four methods, if we go back here
in our bicycle class, these air only set in the bicycle class, and we do not want to write them all over again in our mountain by class. And that is an example for why we would use the inheritance Andi, that we can do by using the extends keyword which will give us all of these properties and methods, which I mentioned a few times. And the reason I'm doing that is that you
get this idea of inheriting properties and methods by the use of inheritance and that extends keyword engraved into your mind and really understand this concept of inheritance.
And then we went over the example that it just mentioned a few seconds ago.
Now in the video electric coming up next door in the lab coming up next, we'll be creating a few classes which will be related to a car, and we will see how this idea of inheritance works hands on by creating and completing this lab.
Now, as always, if you have any questions at all, feel free to connect with me via Ellington. And I would be more than happy to answer any questions. Thank you very much for watching this video lecture, and I hope to see you in the lab coming up next