Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, March 10, 2025

Interfaces; defining an interface, implementing interface.

 

Interfaces in Java

An interface in Java is a blueprint of a class that contains abstract methods (before Java 8) and default, static methods (from Java 8 onwards). Interfaces are used to achieve abstraction and multiple inheritance.


📌 1. Defining an Interface

An interface is defined using the interface keyword. It can contain:

  • Abstract Methods (without implementation)
  • Default Methods (with implementation) - From Java 8
  • Static Methods (with implementation) - From Java 8
  • Private Methods (for internal use) - From Java 9

Syntax

interface MyInterface {

    // Abstract method (implicitly public and abstract)

    void display();

 

    // Default method (has implementation)

    default void show() {

        System.out.println("This is a default method in the interface.");

    }

 

    // Static method (has implementation)

    static void print() {

        System.out.println("This is a static method in the interface.");

    }

}


📌 2. Implementing an Interface

A class implements an interface using the implements keyword. The class must provide implementations for all the abstract methods of the interface.


Example of Interface Implementation

interface Animal {  // Defining an interface

    void sound();  // Abstract method

 

    default void breathe() {  // Default method

        System.out.println("Breathing...");

    }

 

    static void info() {  // Static method

        System.out.println("Animals have various sounds.");

    }

}

 

class Dog implements Animal {  // Implementing the interface

    @Override

    public void sound() {  // Providing implementation for abstract method

        System.out.println("Dog barks.");

    }

}

 

public class Main {

    public static void main(String[] args) {

        Dog dog = new Dog();

        dog.sound();      // Calls the overridden method

        dog.breathe();    // Calls the default method from interface

        Animal.info();    // Calls the static method from interface

    }

}


Output

Dog barks.

Breathing...

Animals have various sounds.


🔍 Explanation

  1. Abstract Method (sound()):
    • Must be implemented by the implementing class (Dog).
  2. Default Method (breathe()):
    • Provides a common implementation that can be overridden by the implementing class if needed.
  3. Static Method (info()):
    • Can be accessed using the interface name itself (Animal.info()).

📌 3. Multiple Interface Implementation (Achieving Multiple Inheritance)

Unlike classes, a class can implement multiple interfaces.


Example of Multiple Interfaces

interface Printable {

    void print();

}

 

interface Showable {

    void show();

}

 

class Demo implements Printable, Showable {  // Implementing multiple interfaces

    @Override

    public void print() {

        System.out.println("Printing...");

    }

 

    @Override

    public void show() {

        System.out.println("Showing...");

    }

}

 

public class Main {

    public static void main(String[] args) {

        Demo obj = new Demo();

        obj.print();

        obj.show();

    }

}


Output

Printing...

Showing...


🔍 Explanation

  • The class Demo implements both Printable and Showable interfaces.
  • It provides implementations for the abstract methods declared in both interfaces.

📌 4. Important Points About Interfaces

  1. Access Modifiers:
    • Methods in interfaces are public and abstract by default (if not specified).
    • Variables in interfaces are public, static, and final by default.
  2. Inheritance in Interfaces:
    • Interfaces can inherit other interfaces using the extends keyword.
    • A class can implement multiple interfaces, thereby achieving multiple inheritance.
  3. From Java 8 Onwards:
    • default methods and static methods can be declared in interfaces.
    • Allows backward compatibility and flexibility in evolving APIs.
  4. From Java 9 Onwards:
    • private methods can be used inside interfaces to provide code reusability within default and static methods.

 

No comments:

Post a Comment