Interfaces in Java:

An interface is a blueprint of a class, containing a collection of abstract methods (methods without a body) and constant variables. It defines a contract that classes must follow when they implement the interface.

Declaring an Interface:

We use the "interface" keyword to declare an interface;

interfaces.java
interface Shape {
    double calculateArea();
    double calculatePerimeter();
}

Any class with interface "Shape"must have implementation of calculatePArea() and calculatePerimeter() methods.

Implementing an Interface:

We use "implements" keyword to implement interface.

interfaces.java
class Circle implements Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double calculateArea() {
        return Math.PI * radius * radius;
    }

    @Override
    public double calculatePerimeter() {
        return 2 * Math.PI * radius;
    }
}

Multiple Inheritance and Interfaces:

Java supports multiple inheritance through interfaces. A class can implement multiple interfaces.

interfaces.java
class Triangle implements Shape, Drawable {
    // Implement Shape methods
    // Implement Drawable methods
}

Default and Static Methods in Interfaces:

Beginning with Java 8, interfaces have the capability to include default and static methods with predefined implementations. Default methods enable the addition of new methods to an interface without causing disruption to existing implementations.

interfaces.java
interface MyInterface {
  // Abstract method
  void myMethod();

  // Default method with implementation
  default void defaultMethod() {
    System.out.println("Default implementation");
  }

  // Static method with implementation
  static void staticMethod() {
    System.out.println("Static implementation");
  }
}

Inheritance with Interfaces:

A class can extend another class and implement multiple interfaces simultaneously. When extending a class and implementing interfaces, the extends keyword comes before the implements keyword.

interfaces.java
// Define an interface
interface Shape {
    double calculateArea();
}

// Define another interface
interface Drawable {
    void draw();
}

// Create a parent class
class Parent {
    void display() {
        System.out.println("Parent class.");
    }
}

class Child extends 
  Parent implements Shape, Drawable {
    private double radius;

    public Child(double radius) {
        this.radius = radius;
    }

    @Override
    public double calculateArea() {
        return Math.PI * radius * radius;
    }

    @Override
    public void draw() {
        System.out.println("Drawing a shape.");
    }
}

public class InheritanceWithInterfaces {
    public static void main(String[] args) {
        Child child = new Child(5.0);

        // Call methods from interfaces
        double area = child.calculateArea();
        System.out.println("Area: " + area);

        child.draw();

        // Call a method from the parent class
        child.display();
    }
}

In this example;

  • We define two interfaces, Shape and Drawable, each with one or more methods.
  • We create a parent class Parent with a display method.
  • The Child class extends the Parent (inheritance from a class) and implements both Drawable and Shape interfaces. It provides concrete implementations for the methods defined in the interfaces.
  • In the main method, we create an instance of the Child class and demonstrate how to call methods from both interfaces and the parent class.