Inheritance

Complete Step By Step Java for Testers Inheritance, Access Modifiers And Abstraction
15 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.59
List Price:  €131.03
You save:  €37.43
£80.45
List Price:  £112.64
You save:  £32.18
CA$136.81
List Price:  CA$191.54
You save:  CA$54.73
A$153.85
List Price:  A$215.39
You save:  A$61.54
S$136.10
List Price:  S$190.55
You save:  S$54.44
HK$783.19
List Price:  HK$1,096.50
You save:  HK$313.30
CHF 91.41
List Price:  CHF 127.98
You save:  CHF 36.57
NOK kr1,095.33
List Price:  NOK kr1,533.51
You save:  NOK kr438.17
DKK kr697.93
List Price:  DKK kr977.13
You save:  DKK kr279.20
NZ$168.49
List Price:  NZ$235.89
You save:  NZ$67.40
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,960.96
List Price:  ৳15,345.79
You save:  ৳4,384.82
₹8,331.14
List Price:  ₹11,663.93
You save:  ₹3,332.78
RM477.70
List Price:  RM668.80
You save:  RM191.10
₦126,037.39
List Price:  ₦176,457.39
You save:  ₦50,420
₨27,728.08
List Price:  ₨38,820.43
You save:  ₨11,092.34
฿3,701.79
List Price:  ฿5,182.66
You save:  ฿1,480.86
₺3,251.67
List Price:  ₺4,552.47
You save:  ₺1,300.80
B$513.17
List Price:  B$718.47
You save:  B$205.29
R1,913.51
List Price:  R2,678.99
You save:  R765.48
Лв183.04
List Price:  Лв256.26
You save:  Лв73.22
₩137,500.16
List Price:  ₩192,505.72
You save:  ₩55,005.56
₪376.08
List Price:  ₪526.53
You save:  ₪150.44
₱5,758.67
List Price:  ₱8,062.37
You save:  ₱2,303.69
¥15,492.10
List Price:  ¥21,689.56
You save:  ¥6,197.46
MX$1,697.24
List Price:  MX$2,376.20
You save:  MX$678.96
QR362.79
List Price:  QR507.92
You save:  QR145.13
P1,387.12
List Price:  P1,942.02
You save:  P554.90
KSh13,448.65
List Price:  KSh18,828.65
You save:  KSh5,380
E£4,787.02
List Price:  E£6,702.02
You save:  E£1,915
ብር5,664.26
List Price:  ብር7,930.19
You save:  ብር2,265.93
Kz83,485.81
List Price:  Kz116,883.48
You save:  Kz33,397.66
CLP$95,360.46
List Price:  CLP$133,508.46
You save:  CLP$38,148
CN¥724.53
List Price:  CN¥1,014.38
You save:  CN¥289.84
RD$5,902.47
List Price:  RD$8,263.69
You save:  RD$2,361.22
DA13,439.62
List Price:  DA18,816
You save:  DA5,376.38
FJ$229.29
List Price:  FJ$321.01
You save:  FJ$91.72
Q776.47
List Price:  Q1,087.09
You save:  Q310.61
GY$20,894.43
List Price:  GY$29,253.04
You save:  GY$8,358.61
ISK kr14,065.59
List Price:  ISK kr19,692.39
You save:  ISK kr5,626.80
DH1,009.26
List Price:  DH1,413.01
You save:  DH403.74
L1,779.82
List Price:  L2,491.82
You save:  L712
ден5,758.50
List Price:  ден8,062.13
You save:  ден2,303.63
MOP$806.12
List Price:  MOP$1,128.60
You save:  MOP$322.48
N$1,913.23
List Price:  N$2,678.60
You save:  N$765.36
C$3,661.78
List Price:  C$5,126.64
You save:  C$1,464.86
रु13,321.12
List Price:  रु18,650.11
You save:  रु5,328.98
S/368.06
List Price:  S/515.29
You save:  S/147.23
K378.12
List Price:  K529.39
You save:  K151.26
SAR375.03
List Price:  SAR525.05
You save:  SAR150.02
ZK2,574.10
List Price:  ZK3,603.84
You save:  ZK1,029.74
L465.65
List Price:  L651.93
You save:  L186.28
Kč2,362.20
List Price:  Kč3,307.17
You save:  Kč944.97
Ft36,810.41
List Price:  Ft51,536.04
You save:  Ft14,725.63
SEK kr1,086.49
List Price:  SEK kr1,521.13
You save:  SEK kr434.64
ARS$87,266.68
List Price:  ARS$122,176.84
You save:  ARS$34,910.16
Bs687.45
List Price:  Bs962.46
You save:  Bs275.01
COP$390,268.69
List Price:  COP$546,391.78
You save:  COP$156,123.08
₡50,117.34
List Price:  ₡70,166.29
You save:  ₡20,048.94
L2,455.43
List Price:  L3,437.70
You save:  L982.27
₲738,156.27
List Price:  ₲1,033,448.32
You save:  ₲295,292.04
$U3,836.09
List Price:  $U5,370.68
You save:  $U1,534.59
zł404.46
List Price:  zł566.27
You save:  zł161.80
Already have an account? Log In

Transcript

Hello guys, welcome back to the series. So today we're gonna discuss a concept called inheritance. So, what does inheritance means? And explain this using a program as usual, I just give you the definition. So it just pretty much sounds like a word right? So, we inherit something from our parents of somebody whose parents I've told us well, you will see that kids are tall.

So, we inherited certain properties from our parents right in our real life also. Similarly in Java, a class can be inherited from a different class. So that means, the derived class the inherited class becomes a subclass to the class from which it was inherited. And the main class becomes a parent class or superclass. So what happens after inheritance is certain fields that are defined in the parent class are methods that are defined in the parent class can be used within a subclass, which is the inherited class. So those instance variables and those methods can be directly used, which means the class inherited those properties, those methods from the pain class.

That's the simple concept of inheritance. So let's go ahead and see it with an example. I'm going to create a new class called in the demo and see right now if you see this is inheriting so not inheriting but the super classes object, right. So which is an ultimate class for every class that's in? That's in the back of Java dot Lang. It's a built in class.

Every class has access to the next To some methods in that object class, which let's ignore it right now, but you'll understand it later. So I just created this class. And let me create another class. Maybe call class cars, always the favorite topic, right? So I don't need a main method inside it. And we created the cars also.

All right. So in my cars method, let me make some in my cloud cost class and make some methods, let's say public wide and crees. Paid. That's khana. Me, let me create a end also, end speed. low speed plus plus.

And see so when does to crazy speed? That should be good, right? So let me create a similar method to say, decrease speed and speed minus minus. So you guys know right if you do speed plus plus it's gonna increasing crease the value of the speed variable. Decreasing speed. That's the functionality of a car right we can increase the speed or decrease the speed.

Now we have a variable also with the namespace. Now what happens? Let me make one more class. And that class my favorite one BMW. I can either specify here the name of my super class which is going to be cars or if not, then I can leave it like that also as see a keyword expense cars so this car says the class that we created here let me create a constructor public cars and something like the Yeah, maybe the default speed so and start speed Maybe let me call it start speed. So it's gonna set speed to start speed.

Start speed. That's it. So what I'm doing in the constructor is just setting my speed to this start speed in my BMW class. All right, so we need to define a constructor for this also. It just says the constructor is not defined, because it extends it extends the superclass. So public, the blue and the red sea or we can you know what we can we don't even need this We can just say this was like, super.

And just give the way, super means it's going to access the superclass. And it's just gonna get the variable. So speed. I can just see. Speed, answer, start speed. So what this does is it's gonna get the constructor from my class, and it's just going to give this value there.

It's going to set it. So I'm just going to give the value from this class, and it's going to call the constructor from a superclass. So by is the key word which is x used to access anything from the parent class. It is my cars class. So now what I can do is so let me create a few objects in this main class, which is inheritance demo. So let's call cars c one equals to new cars.

And let me give us speed and speed equals to zero. Let me pass speed here. And what else I want to do I just want to call maybe see one DOD increase speed. So let me run it now. What happens it says increasing speed. How I can utilize these things inside my BMW class.

Now? That's the question So let me call these things inside that, what I can do is I can simply, first of all, I utilize the constructor from my parent class. Now what I can do is I can just override the methods that are implemented in my hidden class into my subclass. What it means is simply I can use these methods and change the functionality. If I want to use the similar functionality, I can do do that also. But for that, I don't need a method inside my subclass.

But what I can do is I can override the functionality of this method. So I can define a same same method inside the BMW class with the same signature and I can override the functionality I can change the functionality there. So what I can do is either I can type in myself like public wide And crees disappeared and peed in that I can do that. Or I have one more way that I can simply override it the cyclic, go to source override implement methods. What it does is it shows me the parent class because it's extended from the cost class, it shows me the methods inside the car class. If I want to override just to increase speed, I would click on that and click OK. See it overwrites it.

It just says public void increase speed with the annotation override. And there is a to stop. Right now we're just calling the increased speed method from the superclass. So right now it's going to perform the same function So let me do one thing. Let me create increasing speed up cars, or cars so you know the difference. Now let me create one more P and W, b one, or maybe the M w one equals to new BMW.

And let me give speed here. If I do that BMW one dot increase speed What does it do right now let's see. If I run this main class, it says increasing speed of cars. It's called two times and increasing speed of cars. Because first time it's called from here, second time, it's called from here, Buddha times it called the same method, increasing speed of cars. Because I'm just calling the super increase speed.

What I can do now, I can actually speed I can do, I can do this. So let's see. So increasing the, the M w, I can do this. So let's say I've done this now. And what happens if I run this code now? For the first one, it says increasing speed of cars, because this is the object reference from the cars class.

The second one is from the BMW class, which is increasing speed of BMW. So if you guys notice that this has overridden the original increased speed method, and I've implemented a new functionality right here. So this is called enhanced And what else we can do, we can inherit all those trees that's defined in the caste class, we can inherit the speed variable, we can inherit both of these methods are all the methods actually, which are defined in the class class. Mostly. And, well, what else we can do is we can define new methods inside it. So let's say I have some specific functionality public wide, and maybe had sub this play navigation, which is new in BMW.

So I have this functionality, which is new in BMW, and maybe not in most of the cars. Some cars are given now, but not in all the cars. So I want to implement this functionality for my BMW class. So what I would say is the m w specific functionality. So I can call this using b, m w one.bm. My head sub display navigation right around the corner now, it says BMW specific functionality.

But notice guys, I cannot call this method from my seat. If I do, there's no heads up display navigation method available to my class class, because that's the parent class. So, subclass has the opportunity to use the parent class methods, but parent class cannot utilize the subclass method. So I can only Call here in class methods, which are defined in that I cannot use the subclass methods for the parent class. So these are the few things that we can do in inheritance and it's pretty useful sometimes we have, like this is an application that's a cause and we have different makes and models of the class. So, there are some specific functionalities like every class, every car has to start an engine and stop an engine, increase gear, increase speed and those kinds of things.

All these functionalities can be inherited to all the different classes, which are different cars, let's say a BMW. All the basic functionalities can be inherited from the cars class and then the new functionality which is specific to BMW can be implemented in the BMW class similar if we have a Mercedes or Audi or Honda or something else. So this is the benefit of using inheritance. That's all for today, guys. I think it's pretty much to absorb and all you need to do is just create some small classes like this and do some practice. That's all and then in next tutorial we'll cover a few other details in Java.

Thanks for learning guys. Thanks

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.