Tutorials Hut

Java Interface and Implements

An interface is similar to an abstract class or a class which will have declaration of methods to be implemented, some default and static methods and static final variables. In this article we will see details of java interface implements keyword. 

Java interfaces may have no methods or variables at all.

We use the interface keyword in java while declaring an interface. Other classes may implement interface and provide definitions of abstract or declared methods without body.

Important points related to interface:

  • Interface may have static final variables and abstract methods.
  • Interface may have default and static methods.
  • Interface is similar to any class and will generate bytecode class (.class) file after compilation.
  • Before Java 8, interfaces never had any concrete or implemented method inside.
  • Interface is used to define base structure and key methods skeleton which can be implemented by any child class using implement or extend feature of java.
  • Interface provides a high level of abstraction.

Interface Declaration and Example

Syntax:  
public interface {

//static final variables
//skeleton methods’ declaration to be implemented, static and default methods

}

In below interface Car, skeletonM1() method is skeleton without body and will be implemented by another child class, defaultM1() and defaultM2() are default methods, staticM1() and staticM2() are static methods.

Example:

public interface Car {



	String CAR_NAME = "Tesla";

	String MODEL = "M2";



	void skeletonM1(); //To be implimented



	default void defaultM1() { //default method 1

    	System.out.println("I am default method M1");

	}



	default void defaultM2() { //default method 2

    	System.out.println("I am default method M2");

	}



	static void staticM1() { //static method

    	System.out.println("I am static method M1");

	}



	static void staticM2() { //static method

    	System.out.println("I am static method M2");

	}

}

Java Interface Implementation Using Implements

We can implement any interface using reserved keyword “implements’ keyword, a class which will implement interface will be like a sub or child class for interface.

In below example we can see how Car interface is implemented by ElectricCar child class.

Example:

public interface Car {



	String CAR_NAME = "Tesla";

	String MODEL = "M2";



	void startCar(); //To be implemented

	void showPowerSource(); //To be implemented





	default void printCarName() { //default method 1

    	System.out.println("Car name is: "+CAR_NAME);

	}



	static void staticM1() { //static method

    	System.out.println("Car model is: "+MODEL);

	}



}



class ElectricCar implements Car {



	@Override

	public void startCar() {

    	System.out.println("Car started.");

	}



	@Override

	public void showPowerSource() {

    	System.out.println("Car's power source is electricity stored in battery");

	}



	public static void main(String[] args) {

    	Car electricCar = new ElectricCar();

    	electricCar.startCar();

    	electricCar.showPowerSource();

    	electricCar.printCarName();

    	Car.staticM1(); //Can be invoked by interface only



	}

}

Output:

Car started.
Car’s power source is electricity stored in battery
Car name is: Tesla
Car model is: M2

Java Interface extending Another Interfaces

An interface in java may extend one or more interfaces as well.

See below example where MyInterface3 is only extending one interface MyInterface1, but MyInterface4 is extending multiple interfaces. Class which will implement interface will be forced to implement all methods of interfaces along with methods of extended interfaces.

Example:

interface MyInterface1 {

	void method1(); //To be implemented

}



interface MyInterface2 {

	void method1(); //To be implemented

}



interface MyInterface3 extends MyInterface1 {

	void method1(); //To be implemented

}

interface MyInterface4 extends MyInterface1, MyInterface2 {

	void method1(); //To be implemented

}

Tagging Interface

An interface which has no methods or variables only empty declaration is called tagging interface, it is used to create a common standard in coding and may be asked to be implemented by most of related classes.

Example:

interface MyTaggingInterface {}

References

Article from oracle.com: Interface 

Next Articles














  • Leave a Reply

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