Index Page : Link
Donate : Link
Medium Blog : Link
Applications : Link

Need Of Default Methods inside interfaces:
Before Java 8, Every method present inside interface is always public and abstract whether we are declaring or not.
interface BeforeJava8Interf
{
public void t1();
public void t2();
}
Assume this interface is implemented by 100s of classes and each class provided implementation for both methods.
interface BeforeJava8Interf
{
public void t1();
public void t2();
}
class Test1 implements BeforeJava8Interf
{
public void t1() {};
public void t2() {};
}
class Test2 implements BeforeJava8Interf
{
public void t1() {};
public void t2() {};
}
.
.
class Test100 implements BeforeJava8Interf
{
public void t1() {};
public void t2() {};
}
It is valid because all implementation classes provided implementation for both t1() and t2().
Now our programming requirement is that we have to extend the functionality of this interface by adding a new method called t3().
interface BeforeJava8Interf
{
public void t1();
public void t2();
public void t3();
}
If we add new method t3() to the interface then all the implementation classes will be effected and won’t be compiled, because every implementation class should implement all methods of interface.
class Test1 implements BeforeJava8Interf
{
public void t1();
public void t2();
}
CE: Test1 is not abstract and does not override abstract method t3() in BeforeJava8Interf
Hence before java 8, It is impossible to extend the functionality of an existing interface without effecting implementation classes. JDK 8 engineers addresses this issue and provides solution in the form of Default methods, Which are also known as Defender methods or Virtual Extension Methods.
How to Declare Default Methods inside interfaces:
In Java 8, Inside interface we can define default methods with implementation as follows.
interface Java8Interf
{
public void t1();
default void t2()
{
//default implementation
}
}
Interface default methods are by-default available to all implementation classes. Based on requirement implementation class can ignore these methods or use these default methods directly or can override.
Hence the main advantage of Default Methods inside interfaces is, without effecting implementation classes we can extend functionality of interface by adding new methods (backward compatibility).
Need of private Methods inside interface:
If several default methods having same common functionality then there may be a chance of duplicate code (Redundant Code).
public interface Java8ConnectionUtil
{
default void insert(User user)
{
Step1: Connect to DB
Step2: Insert data
Step3: Close connection
}
default void update(User user)
{
Step1: Connect to DB
Step2: Update data
Step3: Close connection
}
default void delete(User user)
{
Step1: Connect to DB
Step2: Delete data
Step3: Close connection
}
}
In the above code all CRUD methods having some common code, which increases length of the code and reduces readability. It creates maintenance problems also. In Java8 there is no solution for this.
How to declare private Methods inside interface:
JDK 9 engineers addresses this issue and provided private methods inside interfaces. We can seperate that common code into a private method and we can call that private method from every default method which required that functionality.
public interface Java9ConnectionUtil
{
default void insert(User user)
{
connection(user, "INSERT");
}
default void update(User user)
{
connection(user, "UPDATE");
}
default void delete(User user)
{
connection(user, "DELETE");
}
private void connection(User user, String crudLevel)
{
Step1: Connection to DB
Step2: Perform the operation based on crudLevel
Step3: Close connection
}
}
Demo Program for private instance methods inside interface:
Private instance methods will provide code reusability for default methods.
interface Java9Interf
{
default void t1()
{
t3();
}
default void t2()
{
t3();
}
private void t3()
{
System.out.println("Common functionality");
}
}
class CodeFactory implements Java9Interf
{
public static void main(String[] args)
{
CodeFactory c = new CodeFactory();
c.t1();
c.t2();
//c.t3(); -> CE
}
}
Output:
CodeFactory> java CodeFactory
Common functionality
Common functionality
Inside Java 8 interaces, we can take public static methods also. If several static methods having some common functionality, we can seperate that common functionality into a private static method and we can call that private static method from public static methods where ever it is required.
Demo Program for private static methods:
private static methods will provide code reusability for public static methods.
interface Java9Interf
{
public static void t1()
{
t3();
}
public static void t2()
{
t3();
}
private static void t3()
{
System.out.println("Common functionality");
}
}
class CodeFactory implements Java9Interf
{
public static void main(String[] args)
{
Java9Interf.t1();
Java9Interf.t2();
}
}
Output:
Common functionality
Common functionality
Note:
Interface static methods should be called by using interface name only even in implementation classes also.
Advantages of private Methods inside interfaces:
The main advantages of private methods inside interfaces are :
- Code Reusability
- We can expose only intended methods to the API clients (Implementation classes), because interface private methods are not visible to the implementation classses.
Note:
- private methods cannot be abstract and hence compulsory private methods should have the body.
- private method inside interface can be either static or non-static.
JDK 7 vs JDK 8 vs JDK9:
1. Untill java 7, we can declare only public-abstract methods and public-static-final variables inside interfaces
interface BeforeJava8Interf
{
public static final variables
public abstract methods
}
2. In Java 8, We can declare default and public-static methods also inside interface.
interface Java8Interf
{
public static final variables
public abstract methods
default method with implementation
public static methods with implementation
}
3. In Java 9, We can declare private instance and private static methods also inside interface.
interface Java8Interf
{
public static final variables
public abstract methods
default methods with implementation
public static methods with implementation
private instance methods with implementation
private static methods with implementation
}
Note: The main advantage of private methods inside interface is Code Reusablility without effecting implemenation classes.
