Default Methods inside Functional interface.
- Until 1.7 version onwards inside interface we can take only public abstract methods and public static final variables (every method present inside interface is always public and abstract whether we are declaring or not).
- Every variable declared inside interface is always public static final whether we are declaring or not.
- From 1.8 version onwards in addition to these, we can declare default concrete methods also inside interface, which are also known as defender methods.
- We can declare default method with the keyword “default” as follows
Syntex of Default Method of Funcational Interface
default void test1()
{
System.out.println("Default Method");
}
Interface default methods
are by-default available to all implementation classes. Based on requirement
implementation class can use these default methods directly or can override.
Example 1. With Default method.
interface TestInterface{
default void m1(){
System.out.println("Default Method");
}
}
public class TestDefaultMethod implements TestInterface {
public static void main(String[] args)
{
TestDefaultMethod defaultMethod = new TestDefaultMethod();
defaultMethod.m1();
}
}
Example 1. With Default method.
interface TestInterface{
default void m1(){
System.out.println("Default Method");
}
}
public class TestDefaultMethod implements TestInterface {
public static void main(String[] args)
{
TestDefaultMethod defaultMethod = new TestDefaultMethod();
defaultMethod.m1();
}
}
Output:- Default Method
- Default methods also known as defender methods or virtual extension methods.
- The main advantage of default methods is without effecting implementation classes we can add new functionality to the interface (backward compatibility).
Note: We can’t override object class methods as default methods
inside interface otherwise we get compile time error.
interface Interf {
default inthashCode() {
return 10;
}
}
Output:- Compile Time Error
Reason: Object class methods are by-default available to every Java class hence it’s not required to bring through default methods.
Default Method Vs multiple inheritance
Two interfaces can contain default method with same signature then there may be a chance of ambiguity problem (diamond problem) to the implementation class. To overcome this problem compulsory we should override default method in the implementation class otherwise we get compile time error.
Example 1.1:
interface Left {
default void m1() {
System.out.println("Left Default Method");
}
}
Example 1.2:
interface Right {
default void m1() {
System.out.println("Right Default Method");
}
}
Example 1.3.
class Test implements Left, Right {}
How to override default method in the implementation class
interfacename.super.m1();
Example 3.
interface Left{
default void m1(){
System.out.println("Left default method implemention");
}
}
interface Right{
default void m1(){
System.out.println("Right default method implemention");
}
}
public class Test3DefaultMethod implements Left,Right{
public void m1(){
//System.out.println("My own Implemention");
Left.super.m1();
Right.super.m1();
}
public static void main(String[] args) {
Test3DefaultMethod method= new Test3DefaultMethod();
method.m1();
}
}
Output:-
Left default method implemention
Right default method implemention
Example 4.
interface Test2Interface{
default void m1(){
System.out.println("Default Method");
}
}
public class Test2DefaultMethod implements Test2Interface{
public void m1(){
System.out.println("My Own implementation");
}
public static void main(String[] args) {
Test2DefaultMethod defaultMethod = new Test2DefaultMethod();
defaultMethod.m1();
}
}
Output :- My Own implementation
0 Comment to "Default Method in Funcational Interface"
Post a Comment