We have studied Interface many times, and as studied that we can declare methods in interface but can not provide implementation. So interface provides 100% abstraction.
But Java 8 was released with a lot of new features and improvements in existing API's.
New features introduced in Java 8 are Lambda Expression, Functional Interfaces, new Time API and new Stream API for bulk operations on collection classes. A lot of changes were introduced in existing API like Default and Static methods in interface, concurrent API changes and a lot other changes and improvement, but we will talk here for Interface changes only. I will create a separate article for Java8 features explanation.
Why Changes required in existing Interface -
As before Java8, method implementation in interface was not allowed. There are several reasons which were considered to make changes in java interface. Suppose we have an existing application and we are using interfaces. Now if we want to add some functionality in interface, it will break the whole application or we need to implement the new changes in all the places that interface is used.
For above reason abstract classes are used in as a default implementation class in many applications so we can add more functionalities in future. One more important reason to introduce default and static method in interface is to support lambda expressions.
Default Method of Interface -
A default method in interface can be defined using default keyword, and implementation class is not required to implement the default methods.
If two interfaces both have default method with same name are implemented by a class, than it is required to implement the default method in implementation class else it will throw an error. (This is required to prevent from Diamond problem with multiple inheritance in Java).
Sample code for default method in interface - In below code sample It is not mandatory for implementing class to override the greeting(String name) methods. But if we want we can override this method.
Static methods of interface are same as default, the main difference between both is that we can not override static methods in implementation class.
Static methods are introduced for specific requirements, where we want to provide a default implementation for all the implementing classes that can not be changed.
Sample code for static method in interface - In below code sample implementing class can not override the greeting(String name) method. This static method will be available only in that particular interface. We can access the method as a static method with interface name like InterfaceWithStatic.greeting("Vijendra") .
I hope Default and static method of interface are clear now. If you have any doubts please put in comments. I will try to resolve ASAP.
***********************Keep studying***********************
But Java 8 was released with a lot of new features and improvements in existing API's.
New features introduced in Java 8 are Lambda Expression, Functional Interfaces, new Time API and new Stream API for bulk operations on collection classes. A lot of changes were introduced in existing API like Default and Static methods in interface, concurrent API changes and a lot other changes and improvement, but we will talk here for Interface changes only. I will create a separate article for Java8 features explanation.
Why Changes required in existing Interface -
As before Java8, method implementation in interface was not allowed. There are several reasons which were considered to make changes in java interface. Suppose we have an existing application and we are using interfaces. Now if we want to add some functionality in interface, it will break the whole application or we need to implement the new changes in all the places that interface is used.
For above reason abstract classes are used in as a default implementation class in many applications so we can add more functionalities in future. One more important reason to introduce default and static method in interface is to support lambda expressions.
Default Method of Interface -
A default method in interface can be defined using default keyword, and implementation class is not required to implement the default methods.
If two interfaces both have default method with same name are implemented by a class, than it is required to implement the default method in implementation class else it will throw an error. (This is required to prevent from Diamond problem with multiple inheritance in Java).
Sample code for default method in interface - In below code sample It is not mandatory for implementing class to override the greeting(String name) methods. But if we want we can override this method.
public interface InterfaceWithDefaults {
void method();
default void greeting(String name){
System.out.println("Hello "+name);
}
}
Static Method of Interface -Static methods of interface are same as default, the main difference between both is that we can not override static methods in implementation class.
Static methods are introduced for specific requirements, where we want to provide a default implementation for all the implementing classes that can not be changed.
Sample code for static method in interface - In below code sample implementing class can not override the greeting(String name) method. This static method will be available only in that particular interface. We can access the method as a static method with interface name like InterfaceWithStatic.greeting("Vijendra") .
public interface InterfaceWithStatic {
void method();
static void greeting(String name){
System.out.println("Hello "+name);
}
}
I hope Default and static method of interface are clear now. If you have any doubts please put in comments. I will try to resolve ASAP.
Check below links also-
How to create Immutable classes in JAVA
Method overloading versus method overriding in Java
Difference between Interface and Abstract class
Difference between Abstraction and Encapsulation
Difference between Comparable and Comparator in JavaHow to create Immutable classes in JAVA
Method overloading versus method overriding in Java
Difference between Interface and Abstract class
Difference between Abstraction and Encapsulation
***********************Keep studying***********************
No comments:
Post a Comment