How to Java programming- Java Interfaces

An interface in the Java programming language is an abstract type that is used to specify a behavior that classes must implement. Java interfaces are similar to protocols. Interfaces are declared using the interface keyword.

In its most common form, an interface is a group of related methods with empty bodies. A bicycle’s behavior, if specified as an interface, might appear as follows:

Java Interface
	interface Bicycle {
		//  wheel revolutions per minute
		void changeCadence(int newValue);
		void changeGear(int newValue);
		void speedUp(int increment);
		void applyBrakes(int decrement);


Java interface is used to achieve full abstraction.By interface, we can support the functionality of multiple inheritances. It can be used to achieve loose coupling.

Java Interface

Java interface


Interface Implementation

All variables declared inside interface is implicitly public final variable or constants. which brings a useful case of using Interface for declaring Constants.

All methods declared inside Java Interfaces are implicitly public and abstract, even if you don’t use the public or abstract keyword. you can not define any concrete method in the interface.

In Java, it’s legal for an interface to extend multiple interfaces.

This is how a class implements an interface. It has to provide the body of all the methods that are declared in the interface.

	interface MyInterface{
	   public void method1();
	   public void method2();
	class XYZ implements MyInterface{
	  public void method1(){
		  System.out.println("implementation of method1");
	  public void method2(){
		  System.out.println("implementation of method2");
	  public static void main(String arg[]){
		  MyInterface obj = new XYZ();
		  obj. method1();


      implementation of method1


Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.


The difference between abstract class and interface.


Abstract ClassesInterface
Abstract class can extend only one class or one abstract class at a timeInterface can extend any number of interfaces at a time
Abstract class can extend from a class or from an abstract classInterface can extend only from an interface
Abstract class can have both abstract and concrete methodsInterface can have only abstract methods
A class can extend only one abstract classA class can implement any number of interfaces
In abstract class keyword ‘abstract’ is mandatory to declare a method as an abstract In an interface keyword ‘abstract’ is optional to declare a method as an abstract
Abstract class can have protected , public and public abstract methods Interface can have only public abstract methods i.e. by default
Abstract class can have static, final or static final variable with any access specifierInterface can have only static final (constant) variable i.e. by default

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...