githubEdit

Day 14 - 04 Oct - Interface

Interface

In Java, an interface is a blueprint for a class, similar to an abstract class but with some key differences. An interface defines a set of abstract methods that any class implementing the interface must provide concrete implementations for. It provides a way to achieve multiple inheritance in Java because a class can implement multiple interfaces. Here's the basic syntax for declaring an interface in Java:

public interface MyInterface {
    // Declare abstract methods  method signatures) here void method1();
    int method2(String str);
}

Now, let's break down the key elements of interfaces:

  1. interface Keyword: You declare an interface using the interface keyword.

  2. Method Signatures: Inside the interface, you define method signatures without providing the method bodies. These are abstract methods, meaning they don't have any code associated with them.

  3. Implementing an Interface: Any class that wants to use an interface must implement it using the implements keyword.

Interface Examples

Example #01 - Multiple Inheritance Using Basic Interface

interface FirstInterface {
    public void myMethod(); // interface method
}

interface SecondInterface {
    public void myOtherMethod(); // interface method
}

class DemoClass implements FirstInterface, SecondInterface {
    public void myMethod() {
        System.out.println("Some text..");
    }

    public void myOtherMethod() {
        System.out.println("Some other text...");
    }
}

// Main Class
public class BasicInterfaceExample {
    public static void main(String[] args) {
        DemoClass myObj = new DemoClass();
        myObj.myMethod();
        myObj.myOtherMethod();
    }
}

Example #02 - Area/Perimeter Calculation of Circle and Rectangle Using Shape Interface

In this example, we define an interface Shape with two abstract methods: area() and perimeter(). We then create two classes, Circle and Rectangle, both of which implement the Shape interface. These classes provide concrete implementations for the area() and perimeter() methods. Interfaces are commonly used to define contracts that classes must adhere to, ensuring a consistent API for various implementations. They're particularly useful when you want to define a common set of methods for classes that may not share the same inheritance hierarchy.

Last updated