Java8 added new features to interfaces, that you can
provide default method implementations
to interfaces using default keyword, and you can add static methods
also.
Default Methods
Default
methods enables to add new functionality to the existing interfaces,
and ensure binary compatibility with code written for older versions
of those interfaces.
By
using default keyword you can specify a default implementation for a
method in interface. All method declarations in an interface,
including default methods, are implicitly public.
Example
interface Car{ void swim(); void drive(); }
class MyCar implements Car{ public void swim(){ System.out.println("I can swim"); } public void drive(){ System.out.println("I can Drive"); } }
Lets
suppose, the Car interface adds new functionality like that all cars
can fly.A
new abstract method 'fly' added to Car interface like below.
interface Car{ void swim(); void drive(); void fly(); }
Previously
MyCar class implemented the Car interface, now with this updated
version of Car interface, if you tries to compile MyClass.java,
compiler throws below error since, MyCar is not providing
implementation for 'fly'.
MyCar.java:1: error: MyCar is not abstract and does not override abstract method fly() in Car class MyCar implements Car{ ^ 1 error
Here
default methods plays a role to ensure binary compatibility with code
written for older versions of those interfaces.
interface Car{ void swim(); void drive(); default void fly(){ System.out.println("I Can Fly"); } }
We
are providing default implementation for the fly method in Car
interface. So it makes your previously existed classes which
implements Car interface works fine.
class MyCar implements Car{ public void swim(){ System.out.println("I can Swim"); } public void drive(){ System.out.println("I can Drive"); } }
class CarTest{ public static void main(String args[]){ MyCar car1 = new MyCar(); car1.swim(); car1.drive(); car1.fly(); } }
Output
I can Swim I can Drive I Can Fly
Extending
Interfaces That Contain Default Methods
When
you extend an interface that contains default methods you have 3
possibilities.
1. If
interface 'B' extend interface 'A', and not providing the
implementations for default methods in A, then extended interface
'B' inherit the default method.
interface A{ void print(); default void show(){ System.out.println("I am in show"); } }
interface B extends A{ void display(); }
class C implements B{ public void display(){ } public void print(){ } }
class Test{ public static void main(String args[]){ C obj1 = new C(); obj1.show(); } }
Output
I am in show
2. If
interface 'B' extend interface 'A', and re-declare the default
methods, then default methods became abstract.
interface A{ void print(); default void show(){ System.out.println("I am in show"); } }
interface B extends A{ void display(); void show(); }
Now
any class that is going to implement the interface B, has to provide
the implementation for the method show also.
class C implements B{ public void display(){ } public void print(){ } }
When
you tries to compile the above program compiler throws below error,
since class 'C' is not providing the implementation for the method
'show'.
C.java:1: error: C is not abstract and does not override abstract method show()in B class C implements B{ ^ 1 error
To
make the program works fine, add the implementation for the method
'show' in the class 'C'.
class C implements B{ public void display(){ } public void print(){ } public void show(){ System.out.println("I am in show"); } }
class Test{ public static void main(String args[]){ C obj1 = new C(); obj1.show(); } }
Output
I am in show
3. If
interface 'B' extend interface 'A', and redefine the default methods,
then default methods are overridden.
interface A{ void print(); default void show(){ System.out.println("I am in interface A"); } }
interface B extends A{ void display(); default void show(){ System.out.println("I am in interface B"); } }
class C implements B{ public void display(){ } public void print(){ } }
class Test{ public static void main(String args[]){ B obj1 = new C(); A obj2 = obj1; obj2.show(); } }
Output
I am in interface B
Interface : static Methods
In
addition to default methods, you can define static methods in
interfaces. All method declarations in an interface, including static
methods, are implicitly public.
interface A{ void print(); static void show(){ System.out.println("I am in interface A"); } }
interface B extends A{ void display(); static void show(){ System.out.println("I am in interface B"); } }
class Test{ public static void main(String args[]){ B.show(); A.show(); } }
Output
I am in interface B I am in interface A
No comments:
Post a Comment