×

Java 9 Interface Private Method

Java 9 provides us the facility to include private methods inside the java interface. In java 8 and earlier versions, we are supposed to use only constant variables and abstract methods inside the interface. So, we are not allowed to provide implementation inside the java interface. We are allowed to declare private methods inside the interface in java 9. This helps to share code among non-abstract methods. The methods which are declared private in the interface are visible only inside the interface. It is used generally when we want to write confidential code. The default methods in the interface allow us to call private methods declared inside the interface. It is useful to remove redundancy by sharing private method code among default methods. The interface private methods need to have their implementation, they cannot be just declared. The static methods have the right to call only private static methods but the default method can call both static and non static methods.

From java 9 and later version versions interface can have:

  1. Constant Variables
  2. Static Methods
  3. Default Methods
  4. Abstract Methods
  5. Private Methods

Java 9 Interface Private Method Example:

interface Demo {  
    default void display() {  
        show();  
    }  
    private void show() {  
        System.out.println(" Welcome to Java 9 ");  
        System.out.println(" Executing private method in an interface ");  


    }  
}  
public class PrivateInterfaceImpl implements Demo {  
    public static void main(String[] args) {  
        Demo s = new PrivateInterfaceImpl();  
        s.display();  
    }  
}  

Output:

Java 9 Interface Private Method

Explanation:

In java 9, we have the facility to declare an interface and include private methods inside it. The interface demo is created with java syntax and one private method show() is created displaying the statements. The default method is created which will call this private method. The main class is created which will implement our interface. Then the object of the interface is created and using the object the default method is called in the interface which will call private method show() and print the statements: "Welcome to Java 9" and "Executing private method in an interface".

Java 9 Multiple Private Method in Interface Example:

interface Demo {  
    default void display() {  
        show();  
        end();
    }  
    
    private void show() {  
        System.out.println(" Welcome to Java 9 ");  
        System.out.println(" Executing private method in an interface ");  


    }  
    private void end()
    {
         System.out.println(" Explore more java 9 features ");  


    }
}  
public class PrivateInterfaceImpl implements Demo {  
    public static void main(String[] args) {  
        Demo s = new PrivateInterfaceImpl();  
        s.display();  
    }  
} 

Output:

Java 9 Interface Private Method

Explanation:

In java 9, we have the facility to declare an interface and include two or more private methods inside it. The interface demo is created with java syntax and two private methods show() and end() are created displaying the statements. The default method is created which will call this private method one after the another. The main class is created which will implement our interface. Then the object of the interface is created and using the object the default method is called in the interface which will call private method show() and print the statements: "Welcome to Java 9" and "Executing private method in an interface". Then it will call the end() method and print the statement: "Explore more java 9 features".

Java 9 Private Static Method in Interface Example:

interface Demo {
   static void display1() 
{
printLines();
System.out.println(" This is method1 ");
   }
   static void display2() 
{
printLines();
System.out.println(" This is method2 ");
   }
   private static void printLines() {
System.out.println(" Starting execution of the method ");
   }
   default void print() {
display1();
display2();
   }
}
public class Test implements Demo{
   public static void main(String args[]) {
Test e = new Test();
e.print();
   }
}  
}  

Output:

Java 9 Interface Private Method

Explanation:

We can also declare static and private static methods inside the interface in java. We have declared one default method calling two static methods which will first call the private static method and then print the statements. The class will implement the interface and using object call the default method print() which will call static methods display1() and display2() which again calls private static methods printLines() which will print the statements starting execution of the methods and then print respective static methods content that is: "This is method1" and "This is method2".


Related Topics

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

InputMismatchException in Java

What is InputMismatchException? One of the most frequent errors in Java is the InputMismatchException. Because the InputMismatchException is a subtype of the java.lang, it is an unchecked exception. RuntimeException.Because it is...

4 minutes read.

Java Custom Exception

Java Custom Exception Java language facilitates us to create our own exceptions. The class intended to throw the Custom Exception has to be derived from the Java Exceptionor RuntimeExceptionclass. The Java...

4 minutes read.

Determine the Upper Bound of a Two-Dimensional Array in Java

Multi-Dimensional Array Multi-faceted exhibits in Java are regular and can be named various clusters. Information in a two-layered bunch in Java is put away in 2D even structure. A two-layered collection...

3 minutes read.

Java Integer numberOfTrailingZeros() method

The numberOfTrailingZeros()  method of Java Integer class returns the total number of zero bits following the lowest-order one-bit in the 2’s complement binary representation of the specified int value. Syntax public static...

1 minute read.

Java Package Keyword

A collection of classes, interfaces, and subpackages in a Java program are referred to as a package. Here, we'll learn in-depth how to make and use user-defined packages. package is a...

6 minutes read.

Java Math acos() Method

The acos() method of Math class computes the trigonometric Arc Cosine (inverse of cosine ) of an angle. The value returned is between 0.0 to pi. Syntax: public static double acos(double a) Parameters: The...

1 minute read.

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

Java Math tan() Method

The tan() method of Java Math class returns the trigonometric tangent of the specified angle. Syntax: public static double tan(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The tan() method...

2 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

Tetris Game in Java

The Tetris game is among the most well-known video games ever produced for computers. Today, we may engage in this game on a mobile device as well. Alexey Pajitnov conceptualized...

12 minutes read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

ConcurrentSkipListSet in Java

ConcurrentSkipListSet is a class that is a part of collection framework in Java.It has been available since Java version 1.6. ConcurrentSkipListSet  is a scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap.The...

16 minutes read.

Java LinkedList

LinkedList Java The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class...

5 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Hashing Algorithm in Java

The hashing algorithm is a method that maps data to the fixed-length hash. The Java hash-based algorithm employs a cryptographic mathematical operation. A hash technique or hash function is supposed...

8 minutes read.