BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java 9 – Private methods in Interfaces (with examples)

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

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

Top Related Articles:

  1. Abstract Class in Java with example
  2. Java Functional Interfaces
  3. What is new Keyword in Java
  4. Constructor Overloading in Java with examples
  5. Static and dynamic binding in java

Tags: Java-9

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

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 Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap