Interface class in java

The Tutorial discuss about the interface class in Java. The interface class in java is one of the core concept of the java programming language and supports the object oriented approach. The java collections, java design patterns, java List uses the implementation of interface classes. The Java Abstract Class provides the partial abstraction mechanism whereas Interface class implements the complete abstraction mechanism.

What is interface class in java ?

The java interface class is declared using the keyword ‘interface’. The java Interface class provides the complete abstraction by providing all the abstract methods for the implementation. With the addition of new features in Java 8 , interface class also provides static methods ( defined with static modifier) and default methods( defined with default modifier) now. Interface class also provides the declaration of constants. Constant values can be defined as public , static and final.

The Java interface class can extends interface or multiple interfaces but cannot extend the class which also concludes that the Java interface class cannot be instantiated but used by other class(es) for the abstract method implementation.

Java Interface Class Signature

The interface class signature includes:

  • The keyword interface,
  • name for the interface class ,
  • parent interfaces (if any) and
  • the interface body ( contains abstract methods, default methods, static methods)

Points to consider while using Java Interface class

  • The java interface class is declared using the keyword ‘interface’
  • The java interface class can extends interface or multiple interfaces but cannot extend the class
  • Java interface class cannot be instantiated
  • Java interface class provides complete abstractions as all abstract methods are without body.
  • Java interface class does not support constructors
  • The sub class requires implements keyword to implement the interface
  • The attribute of the interface is public , static and final by default
  • The abstract methods are by default implicitly abstract and public.

Java interface class example

Lets create an java interface class VehicleInterface.java with abstract methods and then Toyota.java implements the interface.

package core.java.oracleappshelp.basics;

public interface  VehicleInterface {
	
	    //create the interface method
		public abstract void getEnginePower(int enginePower);
		
		//create the interface  method
		public abstract void getVehicleModel(String model);

}
package core.java.oracleappshelp.basics;

public class Toyota implements VehicleInterface{
	private int engineCC;
	private String model;
	
	public Toyota() {
		System.out.println( "This is the implementation class for VehicleInterface");
	}
	@Override
	public void getEnginePower(int enginePower) {
		// TODO Auto-generated method stub
		this.engineCC =enginePower;
		System.out.println( "Toyota Engine Power is="+this.engineCC + "\n");
		
		
	}
	@Override
	public void getVehicleModel(String model) {
		// TODO Auto-generated method stub
		this.model =model;
		System.out.println( "Toyota Model is ="+ this.model);
		
	}
	
		public static void main(String args[]){
		Toyota vehicle = new Toyota();
		vehicle.getVehicleModel("Camry ZX");
		vehicle.getEnginePower(1200);
		}
		
}

Java interface program output:

This is the implementation class for VehicleInterface
Toyota Model is =Camry ZX
Toyota Engine Power is=1200