Java 8 – Types of interface

In this article, we will discuss different types of interface starting from Java 1.0 till Java 1.8 version.

1. interface – before Java 1.8 version

Before Java 1.8 version that is until Java 1.7 version, there are only 2 types of interface,

  1. Java interface
  2. Marker interface

1.1 Old Java interface

This is age-old Java interface which allows 2 things,

  1. Constants which are by default public, static and final
  2. abstract methods which are by default public & abstract even if it is not declared explicitly

OldJavaInterface.java

package net.bench.resources.interfaces.type;

public interface OldJavaInterface {

	// 1. only CONSTANTS allowed, by default public/static/final
	String website = "BenchResources.Net";


	// 2. any number of abstract methods allowed, by default public/abstract
	void writeArticle();
}

1.2 Marker interface

Marker interface are special purpose interface to instruct JVM like,

  1. Classes that implements java.io.Serializable interface allows to serialize & de-serialize Java objects to network storage over wire
  2. Classes that implements java.lang.Cloneable interface allows Java object to be cloned or in simple words original Java object can be copied and create another object

JavaMarkerInterface.java

package net.bench.resources.interfaces.type;

public interface JavaMarkerInterface {

	/** 
	 * Marker interface contains no methods & CONSTANTS
	 * Example - 
	 * 1. java.io.Serializable
	 * 2. java.lngCloneable
	 */
}

2. interface from Java 1.8 version

From Java 1.8 version, there are addtional types of interface

  1. interface in Java 8
  2. Functional interface

2.1 Java 1.8 interface

This is age-old Java interface which allows few addtional things like,

  1. static methods
    • very much same like we define static methods inside class
    • can be accessed using <interface-name>.<static-method-name>
    • any number of static methods allowed
  2. default methods
    • very much same like implementation method inside class
    • can be accessed after instantiating objects like <implementation-class-object>.<default-method-in-interface>
    • any number of default methods allowed

Java8Interface.java

package net.bench.resources.interfaces.type;

public interface Java8Interface {

	// 1. CONSTANTS are allowed, by default public/static/final
	String website = "BenchResources.Net";


	// 2. any number of abstract methods allowed, by default public/abstract
	void writeArticle();


	// 3. any number of static methods allowed
	static void writeStaticArticle() {

		// very much same like we define static methods inside class
		// can be accessed using <interface-name>.<static-method-name>
	}


	// 4. any number of default methods allowed
	default void writeDefaultArticle() {

		// very much same like implementation method inside class
		// can be accessed after instantiating objects like,
		// <implementation-class-object>.<default-method-in-interface>
	}
}

2.2 Functional interface

Functional interface is introduced in Java 1.8 version which allows single abstract method but in addition to this, we can include,

  • any number of static & default methods
  • Constants are allowed
  • To make an interface as Functional interface, we can annotate an interface with @FunctionalInterface which isn’t mandatory
  • Note : if second abstract method is added then compile-time error “Invalid ‘@FunctionalInterface’ annotation; Java8FunctionalInterface is not a functional interface” is thrown because of the presence of @FunctionalInterface annotation

Java8FunctionalInterface.java

package net.bench.resources.interfaces.type;

@FunctionalInterface
public interface Java8FunctionalInterface {

	// 1. CONSTANTS are allowed, by default public/static/final
	String website = "BenchResources.Net";


	// 2. Single abstract method is allowed, by default public/abstract
	void writeArticle();
	
	
	// 3. default & static methods are allowed
	static void writeStaticArticle() {}
	default void writeDefaultArticle() {}
}

Happy Coding !!
Happy Learning !!

Iterating List from JDK 1.0 to Java 1.8 version - 7 ways
Java 8 – How to convert HashMap to ArrayList