Private Methods in Interfaces
Private Methods in Interfaces
1) interface Prior2Java8Interf
2) {
3) public void m1();
4) public void m2();
5) }
Assume this interface is implemented by 1000s of classes and each class provided
implementation for both methods.
1) interface Prior2Java8Interf
2) {
3) public void m1();
4) public void m2();
5) }
6) class Test1 implements Prior2Java8Interf
7) {
8) public void m1(){}
9) public void m2(){}
10) }
11) class Test2 implements Prior2Java8Interf
12) {
13) public void m1(){}
14) public void m2(){}
15) }
16) class Test3 implements Prior2Java8Interf
17) {
18) public void m1(){}
19) public void m2(){}
20) }
21) class Test1000 implements Prior2Java8Interf
22) {
23) public void m1(){}
24) public void m2(){}
25) }
It is valid b'z all implementation classes provided implementation for both m1() and
m2().
~1~
Assume our programming requirement is we have to extend the functionality of this
interface by adding a new method m3().
1) interface Prior2Java8Interf
2) {
3) public void m1();
4) public void m2();
5) public void m3();
6) }
If we add new method m3() to the interface then all the implementation classes will be
effected and won't be compiled,b'z every implementation class should implement all
methods of interface.
CE: Test1 is not abstract and does not override abstract method
m3() in PriorJava8Interf
Hence prior to 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.
1) interface Java8Interf
2) {
3) public void m1();
4) public void m2();
5) default void m3()
6) {
7) //"Default Implementation
8) }
9) }
~2~
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).
Eg:
In the above code all log 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.
~3~
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.
1) interface Java9Interf
2) {
3) default void m1()
4) {
5) m3();
6) }
7) default void m2()
8) {
9) m3();
10) }
11) private void m3()
~4~
12) {
13) System.out.println("common functionality of methods m1 & m2");
14) }
15) }
16) class Test implements Java9Interf
17) {
18) public static void main(String[] args)
19) {
20) Test t = new Test();
21) t.m1();
22) t.m2();
23) //t.m3(); ==>CE
24) }
25) }
o/p:
D:\java9durga>java Test
common functionality of methods m1 & m2
common functionality of methods m1 & m2
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.
1) interface Java9Interf
2) {
3) public static void m1()
4) {
5) m3();
6) }
7) public static void m2()
8) {
9) m3();
10) }
11) private static void m3()
12) {
13) System.out.println("common functionality of methods m1 & m2");
14) }
15) }
16) class Test implements Java9Interf
17) {
~5~
18) public static void main(String[] args)
19) {
20) Java9Interf.m1();
21) Java9Interf.m2();
22) }
23) }
o/p:
D:\durga_classes>java Test
common functionality of methods m1 & m2
common functionality of methods m1 & m2
Note: Interface static methods should be called by using interface name only even in
implementation classes also.
1. Code Reusability
2. We can expose only intended methods to the API clients(Implementation classes),b'z
interface private methods are not visible to the implementation classses.
Note:
1. private methods cannot be abstract and hence compulsory private methods should
have the body.
2. private method inside interface can be either static or non-static.
1) interface Prior2Java8Interface
2) {
3) public-static-final variables
4) public-abstract methods
5) }
2. In Java 8 ,we can declare default and public-static methods also inside interface.
1) interface Java8Interface
2) {
3) public-static-final variables
4) public-abstract methods
~6~
5) default methods with implementation
6) public static methods with implementation
7) }
3. In Java 9,We can declare private instance and private static methods also inside
interface.
1) interface Java9Interface
2) {
3) public-static-final variables
4) public-abstract methods
5) default methods with implementation
6) public static methods with implementation
7) private instance methods with implementation
8) private static methods with implementation
9) }
Note: The main advantage of private methods inside interface is Code Reusablility
without effecting implemenation classes.
~7~