×

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 Final Keyword

In Java, the last keyword is used to limit the user. The applications of the java final keyword have large range of usage in program development. Last can be: variablemethodclass A final...

3 minutes read.

Thread Concept in Java

What is a Thread? A subprocess that is lightweight in Java is known as a thread. It is the smallest unit of a process. For the running of multiple tasks parallelly...

3 minutes read.

Interchange Diagonal elements in Java

In this article, you will be very well acknowledged about what is a matrix with an example. You will also be acknowledged about how to interchange the diagonal elements in...

4 minutes read.

ArrayList VS Linked List

ArrayList VS Linked List Both the ArrayList and LinkedList implements the List interface, and they have some differences as well as some similarities between them. The internal working and performance of both vary significantly. Let’s...

7 minutes read.

Bedrock vs Java

The popularity of Minecraft, a sandbox video game, has skyrocketed. The scope, level of complexity, and variety of gameplay in this game are enormous, and user-generated content has helped to...

4 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Trimorphic numbers in Java

Wondered what the Trimorphic number is!! In this article, you can learn about what a Trimorphic number is referred to as and how to find whether a number is trimorphic...

3 minutes read.

Java copy file

There are for the most part 3 methods for duplicating documents utilizing java language. They are as given underneath: Utilizing File StreamUtilizing FileChannel ClassUtilizing Files class. 1. Using File Stream: Here we are...

5 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

String Array in Java

String Array in Java An array is alinear data structure that stores similar type of data. It allows us to store fixed number of elements.It can be of different data types...

6 minutes read.

Magnanimous Number in java

Magnanimous Number When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left...

3 minutes read.

Moran Numbers in Java

In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers. Moran Number A moran numbers are...

3 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

Block Swap Algorithm for array rotation in Java

An array and r, the rotation factor by which the array must be rotated, are both provided to us. We must then return the rotated array. A well-known and popular method is...

4 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Java Map Example

In Java, the Map is an interface that is used mainly to denote key and value pairs. The central concept and theme of this mapping in the java collection framework...

4 minutes read.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.