❮ Previous Next ❯

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

❮ Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved