Java 8 interfaces and behind the scene
Hello geeks, Java 8 or 1.8 come up with lots of enhancements to survive into market. Since, other programming languages are evolving itself beyond object oriented world.
Since, java until now strictly follows object oriented approach, that's why a lots of programming methodologies (like function programming etc) are still not supported by java until java 1.7.
But in java 8 they started to adopt such programming methodologies too. In this particular blog i am mainly going to talk about the java 8 interface. Since interfaces are there in java from there very first releases, so what new in java 8 interface.
Interfaces in java till 1.7
To get a better clear picture, lets first talk about java 1.7 (or earlier) interface implementation. The main reason behind using an interface is to define a type of user's choice (or vendor's choice).
Just like c programming where we first declare functions and then define them and then use them.
Similarly here we declare methods inside interfaces and then define them inside class and then use them via object of that particular class.
So, as per above observation we know that interfaces can contains only methods declarations i.e. signature of methods within it [ Note that abstract class may contain method declaration and definitions as well ]. The class who implements that particular interface has the responsibility to implement all those declared methods mentioned in that interface. That is the whole concept of interface until java 1.7.
In java 8, Oracle added a lots of new features for example stream API.
Lets consider a scenario of collection interface, the Collection interface added stream method in java 1.8. Collection was there in java since 1.2 and stream is just introduced in 1.8 now problem occurs when a program written in JDK 1.7 or below tries to run on JDK 1.8. So, how to make 1.8 JDK backward compatible.
One way is (i.e. impossible in practical ) implement this newly added method in all those classes which implements this Collection Interface. Which is practically impossible to java developers as well as programmer.
Here, they came up with the idea of default method in interfaces in java.
The author of java.util.Collection package (Josh Bloch, Neal Gafter) made all newly added method(one of them is stream() ) as default with their definations, in this fashion we preserve java 1.8 as backward compatible and newly added feature as well.
Diamond problem due to Java 8 interface concept and it's solution
We know that multiple inheritance is prohibited in java to get rid out of diamond problem. But after 1.8 by support of default method, we again come into a scenario of diamond problem. Where a Class implements two or more interfaces having same methods signature with their defination (default mehods) as well.
To overcome this ambiguity we need to add implementation of the common method into class again.
Point to note here that any method signature matching with object Class's method are not allowed as default method inside interface in java 1.8.
Conclusion : From java 1.8 interfaces may have method signature method, definition and static methods as well to make JDK 1.8 as backward compatible .
Comments
Post a Comment