Ex 1:
interface Left { default void m1() { System.out.println(“Left Default Method”); } }
Ex 2:
interface Right { default void m1() { System.out.println(“Right Default Method”); } }
Ex 3:
class Test implements Left, Right {}
How to override default method in the implementation class?
In the implementation class we can provide complete new implementation or we can call any interface method as follows.
interfacename.super.m1();
Ex:
lass Test implements Left, Right { public void m1() { System.out.println(“Test Class Method”); } public static void main(String[] args) { Test t = new Test(); t.m1(); }
Differences between interface with default methods and abstract class
Interface with default methods
->Inside interface every variable is always public static final and there is no chance of instance variables.
->Interface never talks about state of Object.
->Inside interface we can’t declare constructors.
->Inside interface we can’t declare instance and static blocks.
->Functional interface with default methods can refer lambda expression.
->Inside interface we can’t override object class methods.
Abstract class
->Inside abstract class there may be a chance of instance variables which are required to the child class.
->Abstract class can talk about state of object.
->Inside abstract class we can declare constructors.
->Inside abstract class we can declare instance and static blocks.
->Abstract class can’t refer lambda expressions.
->Inside abstract class we can override object class methods.
Interface with default method != abstract class
Static methods inside interface
From 1.8 version onwards in addition to default methods we can write static methods also inside interface to default utility functions.
Interface static methods by-default not available to the implementation classes hence by using implementation class reference we can’t call interface static methods.
We should call interface static methods by using interface name.
Ex:
interface Interf { public static void sum(int a, int b) { System.out.println(“The Sum:”+(a+b)); } } class Test implements Interf { public static void main(String[] args) { Test t = new Test(); t.sum(10, 20); //CE Test.sum(10, 20); //CE Interf.sum(10, 20); } }
As interface static methods by default not available to the implementation class, overriding concept is not applicable.
Based on our requirement we can define exactly same method in the implementation class, it’s valid but not overriding.
Ex 1:
interface Interf { public static void m1() {} } class Test implements Interf { public static void m1() {} }
It’s valid but not overriding
Ex 2:
interface Interf { public static void m1() {} } class Test implements Interf { public void m1() {} }
This is valid but not overriding
Ex 3:
class P { private void m1() {} } class C extends P { public void m1() {} }
This is valid but not overriding
From 1.8 version onwards we can write main() method inside interface and hence we can run interface directly from the command prompt.
Ex:
interface Interf { public static void main(String[] args) { System.out.println(“Interface Main Method”); } }
At the command prompt:
javac Interf.java
javaInterf
No comments:
Post a Comment