what is the difference in Interface of Java7, Java8 and Java9?

The interface is not only a sort of class with accumulations of abstract methods and constants. It has made considerable progress from where it starts.

Java8 and Java9 has rolled out a ton of improvements in it for simplicity of programming. Lets have a look at its journey.

I. Interface in JAVA7 
It accumulates only two things i.e.

  1. Constants
  2. Abstract methods

There is no scope of a method implementation. Combination of abstract (method without implementation) and non-abstract method (method with implementation) needs abstract class and we can only inherit one class as per standards defined.

****************Java7***********************

interface example{
	int sum=0; // public static final int sum=0; constants
	void fun(); // public abstract void fun(); abstract method
}

 

II. Interface in JAVA8
Oracle Corporation has introduced some new features to Java Interfaces in Java SE 8 Release. That is Default methods and Static methods feature.Now it has 4 things i.e

  1. Constant variable
  2. Abstract method
  3. Default method
  4. Static method

Method can have implementation in Interface from JAVA8 onward by using default keyword before method.

interface example{
	int sum=0; //constants
	void fun1(); //abstract method
        default void fun2(){
        //implementation
        }
}

 

In any case, now consider a situation where most of the functions use the same logic and we want to move that code into one common function. see CODE REDUNDANCY below:-

interface example{
	int sum=0;
	/** redundancy of code**/
	default void one(){
		System.out.println("line1");
		System.out.println("line2");
	}
	default void two(){
		System.out.println("line1");
		System.out.println("line2");
	}
	default void three(){
		System.out.println("line1");
		System.out.println("line2");
	}
}

We cannot write a function and it is public by default, so it exposes the code. Here we need a private function that JAVA9 introduces.

interface example{
	int sum=0;
	/** By default public**/
	void common(){
		System.out.println("line1");
		System.out.println("line2");
	}
	default void one(){
		common();
	}
	default void two(){
		common();
	}
	default void three(){
		common();
	}
}

public class parent implements example{
	/** code exposed **/
	@Override
	public void common(){
		System.out.println("*****");
		System.out.println("*****");
	}
}




III. Interface in JAVA9
To solve above limitation, Oracle introduces the concept of private function in the interface. Now it can have 6 things i.e.

  1. Constant variable
  2. Abstract method
  3. Default method
  4. Static method
  5. Private method
  6. Private Static method

interface example{
	int sum=0;
	/** code not exposed **/
	private void common(){
		System.out.println("line1");
		System.out.println("line2");
	}
	default void one(){
		common();
	}
	default void two(){
		common();
	}
	default void three(){
		common();
	}
}

public class parent implements example{
	/** cannot use common() **/
}

These private methods are useful or accessible only within that interface only.
NOTE:-

  • Private method should always be implemented because it cannot be overriden by extending class.
  • Private and abstract cannot be used together.

Follow Recent Topic – What is Petya Ramsomware?
Read More :-  JAVA9 features Part I
Read More :- JAVA9 Features Part II
Subscribe us.
something is waiting for you. see below


.

Related posts:

3 thoughts on “what is the difference in Interface of Java7, Java8 and Java9?

Leave a Reply

Your email address will not be published. Required fields are marked *