Interface In Java

In Java, an interface is a reference type similar to a class. It can contain:

  • Constants: Variables that are public, static, and final by default.
  • Abstract methods: Methods that must be implemented by any class that implements the interface.
  • Default methods: Methods with a default implementation (from Java 8 onwards).
  • Static methods: Methods that belong to the interface itself and cannot be overridden (also from Java 8 onwards).
  • Private methods: Used internally by default or static methods to provide code reuse (from Java 9 onwards).

NOTE:

An interface is used to achieve abstraction and multiple inheritance in Java.

Key Characteristics of an Interface

  • Abstract methods: All methods in an interface are abstract (unless declared as default or static). This means that they don’t have any body and must be implemented by the classes that implement the interface.
  • Public methods: All methods in an interface are implicitly public, meaning you don't have to specify the public modifier. For example:

    interface MyInterface {
    	// This is implicitly public and abstract
        void myMethod();  
    }
  • Constants: All variables declared in an interface are implicitly public, static, and final. This means you can’t change their value after initialization.

    interface MyInterface {
    	// This is implicitly public static final
        int MY_CONSTANT = 10;  
    }
  • No instance fields or constructors: An interface cannot have instance fields or constructors because it is not meant to represent a specific object’s state. It's more like a contract for other classes to follow.
  • Multiple inheritance: A class can implement multiple interfaces, allowing a class to inherit behavior from multiple sources. This is one of the key benefits of interfaces.

Syntax of Interface

Here's a basic example of an interface:

public interface Drawable {
	// Abstract method
    void draw();
    
    // Abstract method
    int getColor();
}
Code Example
Output
Drawing a circle
Green

In this example, the Circle class implements the Drawable interface by providing concrete implementations for the abstract methods draw() and getColor().


Why Use Interfaces?

  • Abstraction: Interfaces help to define a contract that implementing classes must adhere to, without specifying how the behavior should be implemented. This allows classes to be written in a more general way.
  • Multiple inheritance: Java allows a class to extend only one class, but it can implement multiple interfaces. This provides a way to achieve multiple inheritance of behavior in Java.
  • Polymorphism: Interfaces can be used polymorphically. A class that implements an interface can be referred to by a reference variable of the interface type.

    Drawable drawable = new Circle();  // Polymorphism
    drawable.draw();  // Calls the draw() method of Circle
    
Code Example

Methods in Interfaces (Pre and Post Java 8)

Pre-Java 8:

  • Interfaces could contain only abstract methods (i.e., method declarations without any implementation).

    public interface Animal {
        void sound();
    }
Post-Java 8

Java 8 introduced default methods and static methods in interfaces, enhancing their capabilities.

  • Default methods: These are methods with an actual implementation inside an interface. Classes that implement the interface are not forced to override default methods, but they can if needed.
  • Static methods: Interfaces can also have static methods, which are called on the interface itself rather than on an instance of a class.
Post-Java 9
  • Private methods: Java 9 introduced private methods in interfaces. These are used to share common code between default and static methods within the same interface.

Inheritance in Interfaces

Interfaces support multiple inheritance, allowing an interface to extend multiple interfaces:

Code Example

In this case, MyClass must implement methods from all the interfaces it extends (X, Y, and Z).


Interface Naming Conflicts

  • Method Naming Conflicts: If two interfaces have methods with the same signature, a class implementing both interfaces only needs to provide one implementation.
  • Variable Naming Conflicts: If two interfaces define variables with the same name, a class can distinguish them using the interface name.
Code Example