8.1 Intro to Inheritance

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
4 hours 19 minutes
Difficulty
Beginner
CEU/CPE
4
Video Transcription
00:00
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.
00:16
Now, as always, I've listed out some learning objectives for you to follow throughout this entire video.
00:21
First, we're gonna be going over the high level idea of inheritance and what it entails.
00:28
Then we will be discussing its use in real world programs. And finally we will go into looking an example of inheritance.
00:38
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.
00:50
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.
01:07
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,
01:23
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
01:42
of fruit, and a car is a type
01:46
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.
02:05
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
02:19
a house. And this this I'm talking in terms of java, a language of Java terminology.
02:25
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
02:38
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
02:53
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.
03:06
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.
03:20
So here in the public costs bicycle, we can see we have four methods on three fields and one constructor,
03:28
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.
03:47
We can just inherit these methods and these properties over here
03:52
for our mountain by class, since a mountain bike is a type of bicycle
03:58
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.
04:16
All right, so in this video, we discussed what inheritance is
04:21
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
04:41
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
05:00
fully
05:01
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.
05:15
And then we went over the example that it just mentioned a few seconds ago.
05:19
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.
05:38
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
Up Next