Private methods in interface

In java, Interface is part of abstraction in java and it is one of the strong pillars of OOP. We have already covered a lot of things about Interface in a recent post. Java 8 allows defining default and static methods in the interface. If you want to know, why the default method introduced in java you can read it from here. In this post, we will how java 9 allows us to define the private method and what is the need for the private methods in interface.

Private methods in interface

Before moving further let’s see the evolution of Java interface.

Java 7 and earlier version: Java 7 and before java 7, we can have only two things in the interface i.e. are Constants and Abstract methods. All the abstract method of interface MUST be implemented by classes which choose to implement the interface.

  • Constant variables
  • Abstract methods

Java 8: Java 8 allows us to define default and static methods in interface. But default method was introduced for a special purpose. The purpose was to handle the existing implantation of JDK class. You can read what is need of default methods.

  • Constant variables
  • Abstract methods
  • Default methods
  • Static methods

Java 9: Java 9 allow us to define private methods in interface. It means now encapsulation is possible in interface also. But we should know what the need of private methods in interface is and when we should use it. Let’s take an example:

  • Constant variables
  • Abstract methods
  • Default methods
  • Static methods
  • Private methods
  • Private Static methods

Suppose we have a record of Student. Here we have two default method in interface.

interface Student
{
	default void college()
	{
		System.out.println("Student class");
		System.out.println("Student Rollno");
		System.out.println("Student College name");
	}
	default void game()
	{
		System.out.println("Student class");
		System.out.println("Student Rollno");
		System.out.println("Student game name");
	}
	public void show();
}

public class StudentRecord implements Student
{
	public static void main(String args[]) 
	{
		StudentRecord obj = new StudentRecord();
		obj.college();
		obj.game();
		obj.show();
	}
	
	@Override
	public void show() 
	{
		System.out.println("All information");
	}
}

Output: Student class
Student Rollno
Student College name
Student class
Student Rollno
Student game name
All information

Here you can see both methods have some common code. We can place the common code in one method that will be a private method.

Example in Java 9 – Default methods sharing common code using private methods

Let’s see the how we will introduce a private method to share the common code.

interface Student
{
	private void classDetails()
	{
		System.out.println("Student class");
		System.out.println("Student Rollno");
	}
	default void college()
	{
		classDetails();
		System.out.println("Student College name");
	}
	default void game()
	{
		classDetails();
		System.out.println("Student game name");
	}
	public void show();
}

public class StudentRecord implements Student
{
	public static void main(String args[]) 
	{
		StudentRecord obj = new StudentRecord();
		obj.college();
		obj.game();
		obj.show();
	}
	
	@Override
	public void show() 
	{
		System.out.println("All information");
	}
}

Output: Student class
Student Rollno
Student College name
Student class
Student Rollno
Student game name
All information

Rules for using the private method in interface

  1. We can’t make the Private method as an abstract method. It means we can’t use abstract keyword with a private method.
  2. The private method can be a static method as well. It means a private method can be static and non-static.
  3. We should use private modifier to define these methods and no lesser accessibility than private modifiers.

Leave a Comment