beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java 9 – Private methods in Interfaces (with examples)

By Chaitanya Singh | Filed Under: Java 9 Features

As we know that Java 8 allowed us to create default and static methods in Interface. The intention was to have new methods added to the interfaces without breaking the classes that already implemented those interfaces. Java 9 has introduced another new feature, Java 9 SE onwards we can have private methods in interfaces. In this guide, we will learn why they added this feature, what is the use of it and how to work with it.

Why Java 9 allows us to have private methods in Interfaces?

Java 9 introduced private methods in interfaces to remove the redundancy by sharing the common code of multiple default methods through private methods.

To understand this we have to take an example in Java 8 (without private methods) and then we will take the same example with Java 9(using private methods).

Example in Java 8 – Multiple default methods with duplicate code (common code)
In this example we will see how default methods can have duplicate code which unnecessary increase the lines of code and makes the code less-readable. We will take the same example again using private methods to see how can private methods help us to avoid duplicate code.

interface MyInterfaceInJava8 {
   default void method1() {
	System.out.println("Starting method");
	System.out.println("Doing someting");
	System.out.println("This is method1");
   }
   default void method2() {
	System.out.println("Starting method");
	System.out.println("Doing someting");
	System.out.println("This is method2");
   }
}
public class JavaExample implements MyInterfaceInJava8{
   public static void main(String args[]) {
	JavaExample je = new JavaExample();
	je.method1();
	je.method2();
   }
}

Output:
Java 8 - default methods with common code

Example in Java 9 – Default methods sharing common code using private methods
We are taking the same example that we have seen above. This time we will introduce a private method to share the common code.

interface MyInterfaceInJava9 {
   default void method1() {
	//calling private method
	printLines();
	System.out.println("This is method1");
   }
   default void method2() {
	//calling private method
	printLines();
	System.out.println("This is method2");
   }
   private void printLines() {
	System.out.println("Starting method");
	System.out.println("Doing someting");
   }
}
public class JavaExample implements MyInterfaceInJava9{
   public static void main(String args[]) {
	JavaExample je = new JavaExample();
	je.method1();
	je.method2();
   }
}

Output:

Starting method
Doing someting
This is method1
Starting method
Doing someting
This is method2

As you can see the output is same and the code size has been reduced.

Based on this we can say that the advantages of having private methods in interfaces are:
1. Allows default methods to share common code to avoid duplicate code (redundancy)
2. Improves readability of code.

Java 9 – Private Static methods

Till now we have leaned how to use private methods in interfaces to share common code of default methods. Java 9 also allows us to have private static methods in interfaces.

Since java 8 we can have static methods in interfaces along with default methods. We can not share the common code of static methods using the non-static private method, we must have to use the private static method to do that.

Lets take an example to understand this.

interface MyInterfaceInJava9 {
   static void method1() {
	//calling private method
	printLines();
	System.out.println("This is method1");
   }
   static void method2() {
	//calling private method
	printLines();
	System.out.println("This is method2");
   }
   //this must be static else we will get compilation error
   private static void printLines() {
	System.out.println("Starting method");
	System.out.println("Doing someting");
   }
   default void mymethods() {
	method1();
	method2();
   }
}
public class JavaExample implements MyInterfaceInJava9{
   public static void main(String args[]) {
	JavaExample je = new JavaExample();
	je.mymethods();
   }
}

Output:

Starting method
Doing someting
This is method1
Starting method
Doing someting
This is method2

Here is the screenshot of Eclipse Oxygen.
Java 9 private methods in interface

Comments

  1. rakesh sharma says

    January 1, 2020 at 6:03 AM

    hello, everyone I have a doubt in java 9 features in interface is that if private static method can also used in default method then what is the purpose of private instance method

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java 9 Tutorial

  • Java 9 features
  • JShell
  • Immutable List
  • Immutable Set
  • Immutable Map
  • Private Methods in Interface
  • Try-With-Resources Enhancement
  • Java 9 -Diamond operator Enhancement
  • @SafeVarargs annotation
  • Java 9 - Stream API Enhancements
  • Java 9 Modules

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap