Interface - Abstraction Part 2

Complete Step By Step Java for Testers Inheritance, Access Modifiers And Abstraction
12 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.23
List Price:  €130.53
You save:  €37.29
£79.94
List Price:  £111.92
You save:  £31.98
CA$136.79
List Price:  CA$191.52
You save:  CA$54.72
A$153.20
List Price:  A$214.48
You save:  A$61.28
S$135.94
List Price:  S$190.32
You save:  S$54.38
HK$782.80
List Price:  HK$1,095.96
You save:  HK$313.15
CHF 91.30
List Price:  CHF 127.83
You save:  CHF 36.52
NOK kr1,094.90
List Price:  NOK kr1,532.91
You save:  NOK kr438
DKK kr695.39
List Price:  DKK kr973.58
You save:  DKK kr278.18
NZ$167.81
List Price:  NZ$234.95
You save:  NZ$67.13
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,958.03
List Price:  ৳15,341.69
You save:  ৳4,383.65
₹8,331.80
List Price:  ₹11,664.86
You save:  ₹3,333.05
RM477.70
List Price:  RM668.80
You save:  RM191.10
₦126,689.32
List Price:  ₦177,370.12
You save:  ₦50,680.80
₨27,798.13
List Price:  ₨38,918.49
You save:  ₨11,120.36
฿3,702.20
List Price:  ฿5,183.23
You save:  ฿1,481.03
₺3,249.97
List Price:  ₺4,550.10
You save:  ₺1,300.12
B$514.66
List Price:  B$720.55
You save:  B$205.88
R1,901.16
List Price:  R2,661.70
You save:  R760.54
Лв182.41
List Price:  Лв255.39
You save:  Лв72.97
₩137,421.64
List Price:  ₩192,395.79
You save:  ₩54,974.15
₪379.55
List Price:  ₪531.39
You save:  ₪151.83
₱5,778.57
List Price:  ₱8,090.23
You save:  ₱2,311.66
¥15,552.99
List Price:  ¥21,774.81
You save:  ¥6,221.82
MX$1,705.36
List Price:  MX$2,387.58
You save:  MX$682.21
QR364.16
List Price:  QR509.84
You save:  QR145.68
P1,384.82
List Price:  P1,938.81
You save:  P553.98
KSh13,448.65
List Price:  KSh18,828.65
You save:  KSh5,380
E£4,789.16
List Price:  E£6,705.01
You save:  E£1,915.85
ብር5,677.33
List Price:  ብር7,948.49
You save:  ብር2,271.15
Kz83,741.62
List Price:  Kz117,241.62
You save:  Kz33,500
CLP$95,258.47
List Price:  CLP$133,365.67
You save:  CLP$38,107.20
CN¥724.52
List Price:  CN¥1,014.36
You save:  CN¥289.84
RD$5,872.31
List Price:  RD$8,221.47
You save:  RD$2,349.16
DA13,431.87
List Price:  DA18,805.15
You save:  DA5,373.28
FJ$229.18
List Price:  FJ$320.86
You save:  FJ$91.68
Q776.12
List Price:  Q1,086.60
You save:  Q310.48
GY$20,901.55
List Price:  GY$29,263
You save:  GY$8,361.45
ISK kr13,994.60
List Price:  ISK kr19,593
You save:  ISK kr5,598.40
DH1,013.23
List Price:  DH1,418.56
You save:  DH405.33
L1,780.93
List Price:  L2,493.37
You save:  L712.44
ден5,743.61
List Price:  ден8,041.28
You save:  ден2,297.67
MOP$805.67
List Price:  MOP$1,127.97
You save:  MOP$322.30
N$1,914.68
List Price:  N$2,680.63
You save:  N$765.95
C$3,675.06
List Price:  C$5,145.23
You save:  C$1,470.17
रु13,310.19
List Price:  रु18,634.81
You save:  रु5,324.61
S/370.43
List Price:  S/518.62
You save:  S/148.18
K379.55
List Price:  K531.39
You save:  K151.83
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,608.51
List Price:  ZK3,652.02
You save:  ZK1,043.50
L463.96
List Price:  L649.56
You save:  L185.60
Kč2,349.11
List Price:  Kč3,288.85
You save:  Kč939.73
Ft36,594.60
List Price:  Ft51,233.91
You save:  Ft14,639.30
SEK kr1,086.27
List Price:  SEK kr1,520.82
You save:  SEK kr434.55
ARS$87,315.45
List Price:  ARS$122,245.13
You save:  ARS$34,929.67
Bs689.99
List Price:  Bs966.01
You save:  Bs276.02
COP$389,137.18
List Price:  COP$544,807.62
You save:  COP$155,670.43
₡50,081.85
List Price:  ₡70,116.60
You save:  ₡20,034.74
L2,464.83
List Price:  L3,450.86
You save:  L986.03
₲741,742.01
List Price:  ₲1,038,468.49
You save:  ₲296,726.47
$U3,834.62
List Price:  $U5,368.62
You save:  $U1,534
zł402.27
List Price:  zł563.20
You save:  zł160.92
Already have an account? Log In

Transcript

Welcome back to the next tutorial. So in this one, we're going to talk about interfaces and how what interfaces in Java are and how we can utilize those things. So let's get started quickly, let me open the eclipse. And let's talk about it. So an interface is sort of a reference type in Java. And it's very similar to a class.

So what it is actually, it's a collection of abstract methods. And we have already learned what abstract methods are in our abstract class tutorial, right? So abstract to replace it abstract methods are just the signatures of the method, the name and the parameter types, what it takes and all that it doesn't have the implementation of the method. And the class implements to let's say, I define an interface and it has all the abstract methods but they don't have any implementations, right? So what's the use of it? So he comes to us, any class can implement the interface and the After implements the interface, they are actually signing a contract with the interface that I'm going to implement all those methods that you have defined in yourself.

So whichever class that implements that interface, it's going to implement all the methods that resides inside the interface. So it's going to have all the macro definitions. So that's where innovation comes in to us, because by default, Java does not have a concept of multiple inheritance. So interface comes very handy in that because by using multiple interfaces, one class can implement all those interfaces and can define all the methods available in multiple interfaces. So there it goes in the picture. So a Java interface that cannot implement the method as I already mentioned, but one thing I wanted to focus is, the tutorial that I'm making is based on Java seven or before because in Java eight, they made some changes.

And now interfaces can have static methods. So but I assume because I don't know how many guys might have shipped to Java eight. If you have legacy systems and all those things, Java seven is the most popular right now going on. So up to Java seven interfaces will cover today. All righty. So other than methods, the abstract methods, interfaces can also contain the variables, the constants, and all those sorts of things.

So let's get started and quickly create an interface right now. So new click on interface here and click on class as well. But we can just change it anytime it's just a keyword that we have to change, right. So let's give it a name. We'll give it a name as cars interface, because we're always working with the cars class and all that. So just to keep it simple, let's name it cars interface and click Finish.

So if you guys notice what it did is public interface card survey. So this is the name that we gave. So let's maximize it. And this is the key word that it provided. So if we change it with class, It becomes a class, right? So that's the only difference.

So in your face, this is the car interface, right? So what we can define inside it is some public and very well, so maybe public string, ah speed of the car. So let's give it some value. Maybe I just define it string, that's okay. And what we can define is some methods, right? Public wide, and let's say insurance start.

So engine start is going to have maybe, let's say, thing as, I don't know what it took last time, but we can just take anything. Maybe engine type, and, as it is, maybe a Boolean, let's say is keyless Stop, something like that. So if it's a keyless type, then maybe it has a turnstile or something or if it's, it can accept true or false, right? So that's it. So that's our definition of interface, and nothing goes inside it. So these methods are going to have implementations in the class where the interface is going to be implemented.

Right? So let's talk about implementing an interface, right? So what we need to do is, we need to create a class for that. So a class that implements an interface, it should implement all the methods, right? So let's save this and again, go to our explorer and create a class. Call it interface.

Demo, maybe. So yeah, that's it. So click Finish. And what we need to do is we need to use the keyword called employee, man's and then give the name interface cars. And I can hit control space to see cars interface. So that's, that's there, we just implemented the cars interface, right?

So now I see an error. Let's see what that says there. It says that type interface demo must implement the inherited inherited abstract methods, kind of a start engine start because we haven't implemented any method right now, which was defined, which was sort of defined in class interface, we see this error message, right? So what we can do is either we can manually go and implement those methods, or we can just click on this thing, add unimplemented methods. So it's gonna only add the body for us, like there is a to do task, origin region method stuff. So it doesn't, it doesn't implement it for us.

It sort of creates the body for us to implement. so here we can implement anything, let's say See, so so this is the implementation That's it, right? So that's how we create an interface and we implement an interface, right? So the class does not need to implement the declared variables in the interface. That's one thing. So if you guys noticed that, when I override the method, it had a variable as well, right?

But the class does not have to implement or do anything with the methods. That's good enough. The only constraint is that we have to implement the methods it only goes through the methods right. So once we have implemented this thing, right, once we have implemented the class, for the interface, now we can use we can create an object with the metal also. So let me You know what, let me just create one more class and maybe call it something called interface demo one, something like that, and create a main method. So it is.

So now I have this one more demo one created right? Now notice that it does not implement anything, it does not implement my interface, right. So this is the class that implements interface. Now what I can do is guys, I can create this object with the name of the interface. So let's give the name of the interface called cars interface, something like that. And see my interface.

That's good. Go to the polling and new cars. interface. Sorry, interface demo interface demo. So what we can do is we can give it a type of class interface, but we are actually creating an instance of my class that implements it so I can create an object like this as well, which is perfectly valid. I just wanted to show it to you guys, we can definitely do this thing, you can definitely do interface demo here, which is anyways, it's the normal way to create an object of a class.

But if I want to create an object, I can give the type as interface as well, because my class is anyway, implementing the interface, it's gonna have all the properties, right? So we can just, we can just give this name as well. And now we can pretty much say my intro interface something like that. And God in the sky or something, and engine type is a six cylinder or something, let's say something and it's keyless. Yes, true. We are not using these properties anyway, but we have to define them.

So we can just do that. If I run this method. It should just stay in. This is the implementation Why'd saying this because in interface demo, that's what we printed. This is the implementation, right? So what we just saw is from any other class, we can just define an object of the class that implemented the interface.

And we can use the name as that as well. So that's what I wanted to show you guys. So that's pretty much it. And let's move to the next topic. So next topic is implementing multiple interfaces. So the only thing that we need to do is let's have one more interface let's let's create a interface for maybe BMW Cars.

Maybe they have some specific functionality functionalities. So BMW Cars interface and public wide. And let's see heads up navigation. It is any I don't think it's unique nowadays. I think everything has had sub navigation like Mercedes and all but Just trying to put something. So, the interface should not have body right.

So, we just define the methods like that right. So this is done. Now what I can do is my interface demo, this class can implement multiple interfaces. So here after comma, what I can do is BMW Cars interface. So I can implement multiple interfaces, right, so now I got this as well. So this class is implementing multiple interfaces.

Now, if you guys notice, as soon as I did that, I'm again seeing an error. Now it says the type interface demo must implement the inherited abstract method. So this is the method right had sub navigation. We never implemented it. So again, we can click on Add unimplemented method and it's gone. And we can do something we can we have to define the complete body but just for now, what we can do is we can just print something this this he had sub navigation, right?

That's pretty much it. So this is the way guys that we implement multiple interfaces as well. So this sounds pretty good, right? We the constraint is if we are sort of implementing multiple interfaces, that this class will have to implement all the methods from both interfaces, not just one. So you need to make sure what you're doing but you're in implementing, so all the methods should be implemented. All right.

So this is pretty much it guys. And I think that should be good enough, it should be good enough for practice. I want to talk about one more thing that about the interface variables that although it can contain the interface can contain variables and constants both. But often it does not make a lot of sense to place variables in an interface. Because even if we don't put public here, all the math, all the variables defined in the interface would be public anyways. So if those are constants to be used by classes implementing the interface.

That's okay. But for variables, it doesn't make a lot of sense to define them in the interfaces anyway, they're public. So let's keep them private to a class and let's use them where they're needed. So, at least my opinion is to use the variables inside the class and all the interfaces. But you're free to use whichever way because our allows you to do that. So you're free to do but it's just just my suggestion that it's good to have variables inside the class.

Sometimes you might need them for specific purposes, but mostly just try to have the variables inside the class. But if they are really needed in the, in all the classes that are implementing the interface, if they're needed in all the classes, then just feel free to make them public and put them in the interfaces. So Otherwise, they should be in the classes only. So yeah, that's pretty much it, guys. That's what I'm talking about. So yeah, stay tuned for the next lecture.

Thanks for having this on. Thanks, guys.

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.