×

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

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.

MVC in Java

A well-known design pattern is Model-View-Controller. The discipline of web development. We can organize our code in this manner. The document stipulates that a program or application must include a...

4 minutes read.

Check whether a Number is a Power of 4 or Not in Java

There are many ways to figure out if an integer is a power of 4. This section will go over a variety of techniques for figuring out whether or not...

11 minutes read.

Stable Marriage Problem in Java

Given N men and N women, the Stable Marriage Problem asks you to match up the men and women in such a way that there are never any two people...

6 minutes read.

String Handling Method in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Adapter class in Java

By using the adapter classes, we can implement Listener interfaces. With the help of adapter classes, we can save code as it provides all implementation methods of listener interfaces Advantages of...

3 minutes read.

Java Class Methods

Methods called on a class rather than a specific object instance are known as class methods. The static modifier guarantees uniform implementation across all instances of the class. Syntax public class NameOfClass...

3 minutes read.

Checked vs Unchecked Exceptions in Java

In this tutorial, we will discuss Java's checked and unchecked Exceptions. Exception An exception is an undesirable event that disrupts the normal flow of the program. An exception is thrown at runtime. It...

4 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.

Sublime Number in Java

In this tutorial, we will understand what is meant by sublime number in java. From the point of the coding interview, it is one of the important topics. We will we will...

4 minutes read.

Sales Tax Problem in Java

To calculate the sales tax on a purchase in Java, you will need to know the following information: The cost of the item being purchased The sales tax rate You can then use...

4 minutes read.

Java Comparable Interface

We implement comparable interface when we need a new logic for determining equality. if two objects are equal, the equals() method returns true and compareTo() method returns 0. The basic...

1 minute read.

Java Math log() Method

The log() method of Math class returns the natural logarithmic value for the specified double argument. Syntax: public static double log(double a) Parameters: The parameter ‘a’ represents the value. Return Value: The log() method returns the...

1 minute read.

Types of Assignment Operators in Java

In this tutorial, we are going to study assignment operators and their types in Java language. Before proceeding to the types, let us know the term ‘assignment operator’.  The assignment...

5 minutes read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.