Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

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-1


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 SOFTWARE");
	}
}

java9-1


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 SOFTWARE");
	   display();
	}
	private void display()


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

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

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

}

Output

java9-1

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext