private method in interface

Hii frnds, here we will know an exciting feature of JAVA 9, that is definition of private method in interface. That means it is possible that we can happily define a private method inside the interface.
Interface in JAVA 7
interface Test
{
          void show();
}
Here by default show() method is public and abstract.


java9-interface1

Interface in JAVA 8

When JAVA 8 came to the picture, we defined default and static method inside the interface.
interface Test
{
          void show();
          default void print()
          {
                   System.out.println("JAVA means SILAN TECHNOLOGY");


          }
}
java9-interface2

Interface in JAVA 9

Now JAVA 9 came to the market. Here we can define private method inside the interface. But here one question is arising how private method will be invoked. Bcz private method can’t be called by outside of the interface or class. The scope of the private method is inside the interface or class. So let’s see the following example how private method is defining and invoking.
interface Test
{
          void show();
          default void print()
          {
                   System.out.println("JAVA means SILAN TECHNOLOGY");
                   display();
          }
          private void display()


          {
                   System.out.println("www.java8s.com");
          }

 

}

class Demo implements Test
{
          public void show()
          {
                   System.out.println("SILAN TECHNOLOGY");
          }
}

class InterfaceExample
{
          public static void main(String[] args)
          {
                   Demo obj=new Demo();
                   obj.show();
                   obj.print();
                  
          }

}
Output
java9-interface3
 

 

 

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 0674-2361252

Comment