OK let's just go ahead and write some code on most of the stuff we have learned in the previous lesson 2
about interfaces. 3
OK so let's get started on let's create a new interface called a. 4
So let's just use the same names we have used in the previous lesson. 5
Let's add two methods for one. 6
So these are two abstract methods and recall that we can omit the key words the modifiers public and 7
abstract because they are implicit. 8
OK so they are included by default. 9
So let's just let me just say that. 10
And at this fine and we will say we're in this new folder called interface which is created within com 11
semantics Square. 12
OK. 13
OK. 14
So let me just say right here you are Java. 15
Now let's create a subclass. 16
Let's implement this interface. 17
But let's keep the class as an abstract class so it has to be abstract class it's name it as abstract 18
. 19
And it implements a 20
out of the two methods. 21
Let's just implement one off them. 22
So let me just copy this code and here we are providing an implementation for the Barmouth. 23
And so for the Fruma Third we are not providing any implementation in this abstract class since it's 24
an abstract class. 25
It need not provide implementation for any of the inherited abstract methods but here we are just providing 26
for one of them. 27
So the missive does 28
not look miscreate as such another subclass of this class abstract it and let's call it does x. 29
So this is a concrete class 30
so it extends abstract a under-the implements a. 31
It need not implement it because it's actually extruding abstract and abstractly intent in clemency 32
but let's just leave it like this. 33
Let's just say it does not have any body. 34
And let's not have any code in the body. 35
Now we also have a test class called desk. 36
And here it is the main method. 37
You now you can ignore this method get round and here we are instantiating the interface A which was 38
a compilation error because interfaces cannot be instantiated. 39
Recall that an interface can only be used as a reference type but not as an object. 40
So the compilers and ADO do Senator. 41
This is abstract because A is an interface and it cannot be instantiated. 42
So let me change this to hex. 43
So we are instantiating the concrete class X and we are invoking the two methods foo unbar and let's 44
combine this now. 45
Now it says X is not abstract which is true because X's concrete does not override the abstract motard 46
. 47
Right because we have not yet provided an implementation for the abstract matter with us for the abstract 48
method bar we are inheriting a we are getting the free implementation for them both of them. 49
Abstract. 50
Because I have struck it has implemented bar and X's inheriting it so that's good enough. 51
So for bar we have an implementation for bugs not for food. 52
So let's provide that implementation. 53
So let me just copy this. 54
Change it to who make this class hag's and within X we are printing. 55
For now everything should be fine. 56
You go it's printing foo which is in Class X underspending bar motard the the message from bar which 57
is an abstract. 58
So the bottom of that definition was provided here. 59
Even if this was if it was not implementing a hundred just an independent class an X is still extending 60
abstract A. 61
Even in this case it would still it could do X doesn't have to define but it would still use the bar 62
defined in abstract so everything would work as it is working now. 63
So we discuss this and the lesson in the previous lesson. 64
So let me just compile and run it so it works the way it was working. 65
So let me just say re-inserted. 66
Now let's look at the diamond problem. 67
We know that the diamond problem is not there in Java. 68
So we already discussed it. 69
And so we have a new interface called Beat and it also has this declaration the abstract method fool 70
. 71
He also has abstract motard food and let's just from X let's implement B. 72
So we are doing implementing multiple interfaces here on both the interface and have the same method 73
. 74
And we there is no Diamon problem here because we are just inheriting method declaration but that actually 75
meant the definition is that is provided here in the class X. 76
So there is no chance of a diamond problem. 77
So let me recompile this. 78
I mean exactly. 79
Everything is fine. 80
No let's just go ahead and test the constants. 81
Interface can have a constant. 82
So what are we discussed about constants. 83
Let's just go ahead and do it. 84
So in a I mean just I add 85
this variable called cloud. 86
So implicitly It is actually a static final one. 87
So actually let me just remove this. 88
Let's compile this. 89
So here it gives a compilation error. 90
It says that Val in origin gives a compilation error. 91
That's because the value variable is a static final variable. 92
Iron is started quite and has to be initialized and that did not happen here. 93
So let's go back to a and let's initialize it do you find. 94
And let's go into this. 95
X let me print that value 96
. 97
So now it's printing the value. 98
So that's so we need we have to initialize it for sure if it is a variable we need to initialize it 99
because it's static final and final values have to be initialized. 100
Now let's do just the name clashing of the variables. 101
If you recall we discussed this. 102
So indeed our job also. 103
To declare the same variable. 104
But let's initialize it to a different value 42 and existing it accessing it. 105
But we have to. 106
It's you know we cannot simply compile javac does client because job does cry and we would use would 107
instantiate this X introduced or entia instantiate x. 108
But then I mean it really not recompile X because there is no change in x x remains the same. 109
It's just that our only be has been changed. 110
OK. 111
So. 112
So then we just compile it as a client. 113
It would see if there is any change in x and x is not changed because it's it because we haven't made 114
any change here. 115
So we're just used up all classify loff x. 116
And so we won't see any issue there. 117
So there is no point just compiling test line. 118
What we need to compile is X. 119
OK. 120
You see when we when we compile X then it was in our Edwar checking if there was any change in a or 121
b or abstract a and b b we introduce this new statement. 122
So a recombines beat and then it gave us this compilation error as a reference to what is ambiguous 123
because we we now have rightly defined in both A and B and it has since it has the same name. 124
It is ambiguous so we need to qualify it. 125
So if you recall we need to qualify it like this so let's say we are accessing B. 126
So now we can do this and it works fine. 127
Let's recompile. 128
Slide. 129
That's your own test line and it prints 42 now. 130
So because it we are accessing the 1 and B. 131
So that's named plashing and let's instead of giving a literal 42 Let's actually assign the value 42 132
by making him a method call. 133
So we introduce this method called get around and test blind and all it does is simply write down the 134
value 42. 135
So it's just that the this is a different way of initializing it. 136
And instead of later we are using a method call. 137
OK so let me just go ahead and recompile just glide mean x. 138
Let me recompile X 139
and ask Klein which is not new direction and so it brings the same value. 140
So so that's a different way of initializing a variable and interface. 141
But normally you would just pro-white literally wouldn't be doing this kind of any initialization at 142
least I have never seen it. 143
So that's about a variable initialization and that's about it. 144
And as simple exercise that I want to give you as extruding an interface so let's say I want you to 145
do that. 146
So just create a new interface. 147
We have two interfaces. 148
A and B. 149
So go ahead and create a new interface called C and C should extend it. 150
OK I can just add a new abstract method called foobar. 151
So earlier we had foo on bottle separate but that's just go ahead and add a new method called foobar 152
. 153
And so that's an interface extending another interface. 154
So C interface C is extending a notch adding a new metric or foobar and from X just implement see just 155
do something like this and provide an implementation for foobar and all you can do is just simply print 156
out how and edit print statement column X uncolored foobar. 157
So just provide the method definition foobar within X so that's all I want you to do. 158
And you can find a code hundred code and resources section and it will also include this new interface 159
and that's about it. 160
Farnol