Abstract Class - Abstraction Part 1

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

Welcome back to the next tutorial. So in this one, we're going to talk about abstract classes in Java. So, abstraction is a process of hiding some implementation from the users basically. So when we actually wrote my show all the information to the user, basically, the user is the other class, right? So then we want to have things hidden from the user, right? So only functionality is something that we want to tell them that this is available to you, you don't need to see all the details what's available for, right.

So you don't need to see the implementation of the class actually, you can just see the functionality available. So let me let me try to explain it to the example. So let's say this is a caste class, right? So you don't wanna user to see all the implementation what's happening inside the caste class, right? users want that they know there are methods available, increased speed, or said private speed, or any other method right. So usually One that the other class would only know about the available functionality, which has the methods, and which they should not know how the methods are implemented.

So then that's abstraction that's hiding information. So that's what we want to do. In Java, we can achieve abstraction sort of in two ways. One is the abstract classes, and the other is the interfaces. So in today's when we will talk about abstract classes. So abstract classes, we start with the keyword abstract.

And what I'm going to do is I'm going to create a class right now. So similar to the caste class, I'll create a abstract class so we'll name it abstract cars, maybe, just to keep it simple. We have a name, property name defined right? And just say finished right now. So we have public class abstract class cars, right? What we're gonna do is we're gonna say public, abstract class.

Abstract cars. So this is now our abstract class. When we add the keyword abstract, it becomes an abstract class. So that's how we define an abstract class. So abstract classes can contain abstract methods as well, again, when we define abstract methods will have to use the key whether it's abstract. And but it's not mandatory, it can have without abstract methods also.

But if a class have an abstract method, let's say if I want to have an abstract method defined in this class, then the class must be abstract. So it's the other way. My abstract class may not have any abstract method, it can only have non abstract methods. But if there is one single abstract method, then my class has to be abstract, that's for sure. Right? So that's, so there's one limitation with the abstract classes that we cannot instantiate the abstract classes.

What does It means that we cannot create an object for the object class, the way we have been doing, we cannot do that. But yes, we can extend the object classes. So we can extend the classes, then we can use all the methods available in abstract classes in the child class. So, if we inherit, the NFC class will have to provide the implementations of abstract methods and all those things as well. So let's let's get started. Let's copy some code from the cars class.

Maybe just define a private in speed and just copied here. So that's there now maybe just copied something else as well in our public, so that also sounds good. So what else do we want to copy? Ah, we want to copy. Let's copy all this. Let's copy all this.

So let's copy these constructors as well and we'll change them names. So basically this is again one more thing. This has to be abstract cars because this is a constructor, so it has to match the name of the class. Again, this has to match the name of the class. All set. So now we have defined the constructor as well.

So it is all just similar. So what I want to do is I want to just use one of the methods from the caste class and put it inside. So this is my method, it says set private speed, right? You know what, let me let me write a method, maybe. Let me just make it a good speed or something. So just leave it No problem.

So now this is my abstract class, right? So we're setting the private speed. Now what I want to do is I want to create one more class. So maybe, let's create one more class where I'm going to use the abstract class. So let's name it abstract demo. Maybe some some time class.

Maybe p Blue. So now we have an abstract demo BMW class, and click Finish. So what I want to do inside is is that I want to use the abstract class. And I want to see what how I can utilize it. So first of all, let's do something. Let's create a main method.

So public, static, void main. And now provide the arguments. So string arcs, right, so this is our main method. Now what I want to do is I want to use the object class, right? So how to use the offset class. Let's use that name, abstract and costs and create an object called C one new abstract.

Toronto cars. done right? So that's how I created an object. I don't need this. So I have the object Right. So what does it say?

Cannot instantiate the type abstract cars. So it right away gives us an error that you cannot instantiate the abstract. abstract class class. Why? Because it is an abstract, right? It is an abstract class, here it is.

So let's remove it just just for fun. Let's remove it, save it. And let's see, does this see this just goes away, because now it's not abstract anymore. So that's what I wanted to show you guys the abstract class cannot be instantiated. So, this is gone, we cannot instantiate it then. So now how to use the abstract classes.

To use the abstract classes what we have to do is we have to inherit those. So to inherit the abstract classes, we have to extend it so abstract demo BMW call now extends and abstract. So basically, we are now extending the abstract class. So this is the simple way that we can use all the methods inside Check class. And that's, that's perfect, that's perfect to do. So there's no issues with that you can do all the things, you can pretty much use the methods inside the abstract class as well.

So I'll go render. So what I can do is instead of this mainstreaming method, what I can do is I can show you a few things like we have a method called public rights, that pride speed, right? So now, what I can do is I so basically the, the kind of purpose of abstract classes. Let's say I have a abstract class, which has cars, and then I have certain different types of cars. Now I know that they share some common functionality that they have. They all have engines, let's say engine start the it's a common functionality.

So everyone needs that method, right? So why don't if the implementation is same, then why don't just put that right. So all we can do is we can just on Create here public. And we can just have the same name as well. Wow. Let's see the Yeah, it's just a wide, so it was a private speed and is providing a speed.

So end. Sorry. So in speed. And let's put this. So what I can do is I can just say super, right so.so private speed, and then pass into. So what I'm here doing is I'm calling the superclass method.

So the class is the parent class. And I'm calling this method passing intent. So basically, I'm setting the private spirit. So basically, this is the private speed, but just assume that this is a method to start the engine. So I'm just starting the engine. So where all the functionality is common.

I don't need to do anything today. Friend, no fancy words, then what we can do is we can have one common object class and inherited in all different child classes and use all those functionalities. So this is the purpose of abstract classes. And what we can do is if we want to override some functionality that let's say, I have a BMW and then a Honda, so my BMW has that start. It doesn't need the key it has a keyless start. So what I can do is I can something like that, maybe let's say let me just let me just write a method called public wide engine, Todd.

And it's actually not doing anything to undersea. So is totted? Right. So it says that nothing else. So what I can do in object class, my BMW classes, I can have the same method public wide in turn, Scott and here What I can do is I can use the super method, and super dot engine start. And then whatever more functionality that I want to do, let's say I have some more functionality because it's keyless entry.

Or maybe it's scale as engine start. So P and W's have units in stock. So this is the additional functionality that I implemented, right? So this is in addition to what I have in the super method, which is this, right? So in addition to this, I can have more functionality implemented in my child classes. So this is the whole purpose.

So let's say I have Mercedes I have Honda's and I have different classes and every once years some common functionality and then they modified based on their needs, so they can just completely override the method as well. So that's also they can do so there is the whole purpose of abstract classes to have some common features put up in the in one spot and then utilize in different classes. That's how they're useful. And it's really very helpful. So that's what abstract classes are. So what I'm going to talk about next is just a very simple thing, abstract methods, because that's a part of object class, I wanted to talk about them, those as well, parents only.

So what abstract methods are, again, we need to have an abstract keyword. So let's say again, use this method. So what we can do is public and then after that, we have to say abstract here to make them abstract. But the difference is that they only contain the method signature, and then should not have any body. So by that what I mean is, this is the body of the method right? So they should not have any body, this will be gone.

So the body is gone. If the body is gone, we don't need the curly braces as well. So this has gone and this has just ended up like with the semicolon. So this is my abstract method. The method ignition would be here my arguments can also go like end key type, maybe a string key, right? Something like that, I'm just making a thing.

So, key type are and in how many cylinders number of cylinders in the car. So, this is let's say engine start method right. So, what I want to do with those is instead of implementing something here I just want to define abstract methods. And the consequences of this is that any class that is inheriting the abstract class this my this class, any class which will inherit my abstract class class will have to implement the abstract methods. Whatever is declared they have to implement it by implementing what I mean is they have to define those methods inside themselves and then implement the funnel analogy. So let's see you guys, you guys must already be saying this right?

There are already hundreds, which is the type abstract demo BMW must implement the inherited abstract method, abstract car start engine start. And it already is showing this thing, right? It's already showing this thing. So what you can do is you can just say, add unimplemented methods, or you can implement on your own as well. So when I'm just gonna say this, so let's comment this out. Because this is something else.

So let's comment this out. So yeah, this is commented. So you guys notice what's happening. It's overriding the method that's here, because it's going to override the functionality as nothing is there. And it's just a stub that's generated, that something is forbidden. So we can leave it empty if we don't have any functionality.

But once the signature is here, the class is not complaining anymore, right. So that's important. Let's say you have some functionality and you want that I need that functionality to be implemented by the classes, whoever is, whoever is extending my abstract class, right? So you want that method to be abstract. So any class that's gonna inherit this class that's going to extend this class, we'll have to implement it. So BMW might start differently.

And Mercedes might start differently. So they might have different implementations, right? So what you want to make sure is you want to make sure that there is a method implementations could be completely different depending on the classes. But yes, I want to make sure that this method gets implemented. So you make those methods abstract method. So yeah, this is the BMW engine, BM, W engine, star, I are some other class, which might have some other way so they can implement it differently.

So that's pretty much it, guys. And that's what I wanted to show today in this lecture. So yeah, just to rephrase abstract classes, they cannot be instantiated, no object can be created. But yes, they can be extended. So we extend it using this. And then we can use all the methods available in the parent class in the child classes.

And in case we decide to create some abstract methods, they can only have method signature, no body to the method, and then they need to be implemented in the class that is inheriting the abstract class. So yeah, that's pretty much it guys. So what I would suggest is practice practice as much as you can, I will upload these lectures on the web. So also, I will upload these classes, these files on the web with the lecture, so please have a look at them. Practice as much as you can. And stay tuned for the next lectures guys.

Thanks for watching this one.

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.