In this topic, we will discuss the private
and private static methods
inside an interface.
Table of Contents
interface in Java is a concept which is used to achieve abstraction. In early versions of Java, an interface
can contain only abstract methods and constants until Java 8 which allows to add default and static methods as well.
In Java 9, interface
allows creating private and private static methods
. It means now an interface can have the following:
- Abstract methods
- Constant variables
- Default methods (added in Java 8)
- Static methods (added in Java 8)
- Private methods (added in Java 9)
- Private static methods (added in Java 9)
Now, let’s understand what is private method and why it is important.
Interface Private Method
A private method
declared inside an interface is similar to declared inside a class. It is declared using private access modifier to keep it limited to the interface. These methods can’t be accessible outside the interface
and don’t inherit to the interface or implementing class.
The primary purpose behind to add private method was to share common code between non-abstract methods within the interface. Let’s see an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
interface Drinkable{ default void breakTime() { coffee(); } private void coffee() { System.out.println("Let's have a Coffee"); } } public class Main implements Drinkable{ public static void main(String[] args){ Main main = new Main(); main.breakTime(); } } |
Output:
Interface Private Static Methods
Like private instance methods, private methods can be static as well. A private static method can not be accessible from outside the interface and can only be accessible inside the default or static methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
interface Drinkable{ default void breakTime() { coffee(); tea(); } private void coffee() { System.out.println("Let's have a Coffee"); } private static void tea() { System.out.println("Let's have a Tea"); } } public class Main implements Drinkable{ public static void main(String[] args){ Main main = new Main(); main.breakTime(); } } |
Output:
Let’s have a Tea
If we want to share code between instance methods
, private instance methods and private static methods both can be used. But If we want to share code between static methods
, use private static methods only. See, what happens, if we try to access a private method
inside a static method
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
interface Drinkable{ static void breakTime() { coffee(); // Error : Cannot make a static reference to the non-static method } private void coffee() { System.out.println("Let's have a Coffee"); } } public class Main implements Drinkable{ public static void main(String[] args){ Main main = new Main(); //main.breakTime(); } } |
Output:
Key-points to remember
Interface private methods
must have implementation body; they can’t be declared as abstract methods.- A
static method
can call only private static method, but a default method can call both the static and non-static(instance) private methods. - The
interface private methods
support sharing common code between non-abstract methods(default, static and private).
Conclusion
This topic is all about the private methods in the interface
. This feature makes the interface more powerful and advance. Now, interface supports both private and private static
methods within an interface.
That’s all about Java 9 Private methods in interface.