8.22 Interface in Java 8 Default , Static Methods | New features

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back aliens this is having ready from political learnings and in this video we'll talk about interfaces but not just interface we'll talk about interface in java 8. now you know interface is there from very long time right of course when you started with java we have interface and the main reason why we use interface is to define the type of the class example let's say you want to go for so if you remember the concept of c programming in c programming uh whenever you want to define a method what which or function we used to first declare the function we should define the function and then we used to use the function right of ways to call the function in java if you do the same thing we use interface where you can declare the methods uh in class you define it and then you call it right so we use interface in lots of applications so if we talk about different inbuilt classes and interface in java there are lots of interface which are inbuilt right so we use interface so that's what's what's new in that so if we talk about interface so we have this concept of we can define an interface and in this interface so let's say this interface name is i so in this interface you can only declare methods right example let's say if you if you talk about a method called show so this method will be by default public abstract right so this will be let's say if i say void show so by default it is public and abstract so even if you don't mention it even if you if you're not writing this part it is by default public abstract and that's the main difference between abstract class and interface right so on the other hand if we talk about abstract class we have this concept of abstract class here in which you define a class and you make that class as abstract right this is a is abstract and then let's say class name is uh abc and what we do is in this class you can you can declare methods and you can you can define methods right right so we can also write some method which has definition that's what the main difference between interface and class right so in abstract class we can make the method abstract or we can also have a defined method but in case of interface it should be only abstract methods right now with so interface will have only abstract methods so that was the concept of interface till java 1.7 so from 1.0 to 1.7 we cannot define methods and interface we can only declare it which is abstract methods so by default all the methods or of interface will be public abstract but the problem is in java 8 so in java 8 we got lots of features right so we got of we got one of the feature called as stream api right so again what is stream api that's a different thing but we are we have a concept of stream api in which you have certain methods of stream api so you have methods like uh stream itself so stream is a method now this stream method was introduced in java 1.8 okay it was not there before and it was introduced in one of the interface called as list so list is a interface in java which we use which we use in collection api right so whenever you work with collection we use least we use at a least we use linked list right so we have this list interface and in this list interface it is there from 1.2 so this interface is there from 1.2 right and it has certain methods so let's say it has x methods so initially when list was created it has x methods but in java 1.8 we got some extra methods so now we have x plus y methods so this x methods which are there from 1.2 and then we have this y methods which are related to stream api not just stream we have lots of methods right which is which is built using stream api right now the questionnaire is should we change the interface when it is published example if one let's say if one of your friend or earlier you you're working with a software in which you're working with 1.7 right and in 1.7 we have x methods and now you're using is using the same software on 1.8 where you have some extra methods which are abstract then unfortunately your class will implement all the methods that's not possible right because you have not changed the software you have just changed the java version so it will not work so what uh oracle has done it in java 1.8 what they did instead of declaring these methods inside inside list as abstract they have defined it hold on can we define a method inside interface is it possible uh that means we can define an interface so that's your interface and let's name it as list that's the input interface right so it has some methods like it has method like add which is abstract then we have a method like uh remove which is abstract and then we also have a method called a stream now we cannot make this method as abstract because not every class which is 1.7 1.7 we don't have this implementation right so how would it work so they have this said this is not the declaration you can actually define a method which means you can say void stream no need to declare it you can define the method inside interface and hold on is it possible to define then even if it is possible then what's the difference between interface and abstract class then because in both the things we can declare and we can we can define right so let's talk about how this definition works here how to define a method inside interface so this whole session so we'll talk about this uh how to define a method inside interface now so let's talk about that so in order to define the interface in order to define a method inside interface what we can do is we can create an interface so we will say this interface name or let me just take uh some extra area here okay so let me just take here okay yeah so i have uh interface i will name this interface as i as usual and in this interface i can declare a method and also also define a method so let's say if i want to declare a method i will say i will say void uh let's say the declared method is let's say add so we have this method which is declared which is public abstract right but we can also define it so we can let's say let me get one more method here which is void show right now this method i want to define it so what we'll do is we'll in order to define this we just have to write default keyword here okay if you just write void so it will not work you just have to mention one more keyword which is default so that means inside interface you can define a method if that is default okay you have to use this default keyword and once again this code this code will only work in 1.8 so let's say if you're using netbeans if you're using eclipse you have to make sure that your jdk version is 1.8 okay so yeah that's how you can define it so that's how you you define the you uh create a default method interface now there's of this one little problem here you know uh in java we doesn't we don't have a concept of multiple inheritance is because it creates a diamond problem right and what is the diamond problem so let's say we have let's say we have two classes uh we have class a and we have class b and in class a we have a method which is show in class b also we have a method which is show and then we have creating class c and let's say if class c extends only class a it is not extending class b now so if class c extends class a and if i create the object of class c as obj and if i say show now since in class c we don't have a method which is show so it will go to class a right that's how it works but let's say if class c now extends class b also that's multiple inheritance right so there will be a confusion which is an ambiguity ambiguity which show to call right so that's why java says no will not go for multiple inheritance here so multiple inheritance is not allowed in java but it is it is applicable with the help of interface right so what we can do is we can have two interfaces i and j both are interface right both are interface uh and in both this method in both this class and in both this interface we have a method with show and let's say if i create a new class which is c so this implements this is not extend this is implements both the interfaces now in this scenario if i create the object of c it is compulsory to have that method show here right because they are declared that they are not defined so we can implement that but now since in interface we have the definition right so let's say now this show is defined here so this show is defined here so need to define it here right so that now if i say if this c does not implement j only implements i now i can simply call obj dot show no need to override that method here because normally interface what we do so if you have an interface in your class what we do we we we override those methods right but since in i in interface i we already defined a method which is show we don't have to define it here right that's the advantage and we can simply say object show but since in java in java we can so one class can implement two interfaces right so we can simply say class c which implements which implements i comma j so if you are doing this now the the problem with other is now because we have a class which implements two interface and in both the interfaces example we have interface i and we also have interface j in which you have a method which is show which is defined right so this will also create a problem now okay then you have to solve this dependency again you if you write this statement if you write this statement in java 8 now it will give you error okay provided in i and j we have this uh methods defined okay so now so uh java says we don't we don't support multiple inheritance because of diamond problem and in java 8 we have the same problem again and to solve that what we can do is in this c also we have to define show so if you have the confusion it is your responsibility to define that method here okay in this scenario since obj dot show obj is a method of c now it will call show there will not be any confusion okay so let me repeat if you're not if you're not implementing j you're just implementing i no need to define show here because it is defined in interface using default keyword but if you are implementing two interfaces and both this interface has the same method to show which which is defined using default keyword you have to override in c also sounds good and then we can say obj.show which is the object of c and we can call it provided you have this show method here okay uh now let me go for one more thing what if uh let me just create a scenario so let's say we have interface i okay and we also have interface j just imagine we have one more interface here which is interface j and the code for j is exactly same as code of i okay same same code now and we have a class which is a now this class a doesn't implement anything okay nothing nothing it is just a pojo and in this i'm saying public void show okay so this is also show this is also show and this is also in this also we have show right and then we have class c which is this this c extends a okay so we have a class a here and now in this class c we already got show right what happens if i say obj dot show let me let me just print here so in this out i'm printing let me just write sop which is system.printer and i'm printing i right in this sop i'm printing j and in this in this i'm printing sop as a so we have i j and a okay so if i'm creating the object of c which extends a if i say obj dot show what will be the output of course a right because we are just extending with a but what will happen if i say implements i now since in i also we have show in a also we have show now which which method it will call so in this scenario the output will be a because class has more power compared to interface so if you have a class in which you have a method this method will will hide this method here so default will have the low priority and normal methods will have higher priority okay this is also called as third rule so this this comes under a topic called as third rules in java okay so so that's how it works so if you have a class which extends a class an interface then it will give the priority to the class method not the interface method sounds good so that's one of the thing in default methods now we have one more concept here in default the concept is what if we have a interface and in this interface we have a method which is public boolean equals and which accept object is o and it says return true now question analyze will it work or not public by default right because we are working with interface and inside interface we can only have this default keyword right so will it work we are we are defining and a method inside the interface is it possible to define this method uh technically yes because if you have you can define methods in java right in java 8 if it is interface providing it is provided we have use we are using default keyword but the problem is this definition here this signature method name is equals which accepts the object as a parameter which returns a boolean value this is a method which is already there inside object class right so if you have any method in your interface which overwrites object class methods then this will give you error okay this is not possible so these are certain things you have to remember when you work with default methods and interface okay so that's your default methods if you have any questions you can put in the comments section so that i will rectify it using the uh screen recording so i will type this inside inside eclipse and i will show you the error exact data if you have any questions with that okay so that's the default methods next we'll talk about is static methods now if you are working with java 1.6 or 1.7 static methods were not allowed but now since we are working with java 8 it supports static methods okay that means you can create an interface which is let's say this is interface i and in this interface are you can actually define methods which are static example we can say static void and you can say this is show you don't even have to use that default keyword here right and now the advantage will be if i say sop hi now since this is static in your main function so let's say we have a main function here inside your main function you don't even have to create the object of i because that is we have a static method we can simply say i dot show it is that powerful right so yeah so that's how we can use uh interfaces we can use the static methods inside interface in java 8. again if you try to run this code inside 1.7 it will not work it will only work in 1.8 okay so this is this is the new features in interfaces in java 8 okay so which is default methods and static methods so that's it from this video if you have any questions you can use the comment section uh to ask your question and uh you can you can ask any question in fact we have started with the qa session so normally every sunday will have this maybe any day any week i will give you the two days notice if you have any uh qs sessions so you can take the uh you can come to that q a session you can ask your own questions i will try to answer those questions for that first you have to subscribe the channel so that you will get to know that i'm going for a qr session so that's it thanks for watching and do subscribe for further videos
Info
Channel: Telusko
Views: 500,899
Rating: undefined out of 5
Keywords: Interface in Java 8, New Features in Java 8, navin, reddy, naveen, telusko, java, tutorial, java 8, lambda, static methods in java, java 8 interface, java 8 new features, interface in java, interface in java with example, java 8 tutorial, java 8 lambda expressions, java 8 streams, java 8 streams tutorial, java se 8 new features, java 8 new features interview questions, java 8 new features video, java 8 new features youtube
Id: 4e_RsZWdiSc
Channel Id: undefined
Length: 18min 20sec (1100 seconds)
Published: Mon May 02 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.