Interface In Java

Interfaces in Java An interface in Java is a reference type that contains only abstract methods (until Java 8), default methods, static methods, and constant declarations. It's a way to achieve abstraction and multiple inheritance in Java. Key Characteristics of Interfaces All methods are abstract by default (before Java 8) Cannot be instantiated directly No constructors All fields are public, static, and final by default A class can implement multiple interfaces Basic Interface Syntax public interface Vehicle { // Constant (implicitly public, static, final) int MAX_SPEED = 120; // Abstract method (implicitly public and abstract) void start(); void stop(); // Default method (Java 8+) default void honk() { System.out.println("Honking the horn"); } // Static method (Java 8+) static int getMaxSpeed() { return MAX_SPEED; } } Implementing an Interface public class Car implements Vehicle { @Override public void start() { System.out.println("Car started"); } @Override public void stop() { System.out.println("Car stopped"); } // Can override default method (optional) @Override public void honk() { System.out.println("Car horn: Beep Beep!"); } } New Features in Later Java Versions Java 8 Additions Default methods: Provide default implementation for methods Static methods: Utility methods related to the interface Java 9 Additions Private methods: For code reuse within the interface Multiple Inheritance with Interfaces interface A { void methodA(); } interface B { void methodB(); } class C implements A, B { public void methodA() { /* implementation */ } public void methodB() { /* implementation */ } } Interface vs Abstract Class Feature Interface Abstract Class Methods All abstract (pre-Java 8) Can have concrete methods Variables Only constants Any variables Inheritance Multiple Single Constructors No Yes Access modifiers Public by default Can be any State No instance state Can maintain state Functional Interfaces (Java 8+) An interface with exactly one abstract method (can have multiple default/static methods): @FunctionalInterface interface Calculator { int calculate(int a, int b); default void printResult(int result) { System.out.println("Result: " + result); } } Interfaces are fundamental to Java's design and are widely used for abstraction, polymorphism, and to define contracts that classes must follow.

Apr 29, 2025 - 19:16
 0
Interface In Java

Interfaces in Java

An interface in Java is a reference type that contains only abstract methods (until Java 8), default methods, static methods, and constant declarations. It's a way to achieve abstraction and multiple inheritance in Java.

Key Characteristics of Interfaces

  1. All methods are abstract by default (before Java 8)
  2. Cannot be instantiated directly
  3. No constructors
  4. All fields are public, static, and final by default
  5. A class can implement multiple interfaces

Basic Interface Syntax

public interface Vehicle {
    // Constant (implicitly public, static, final)
    int MAX_SPEED = 120;

    // Abstract method (implicitly public and abstract)
    void start();
    void stop();

    // Default method (Java 8+)
    default void honk() {
        System.out.println("Honking the horn");
    }

    // Static method (Java 8+)
    static int getMaxSpeed() {
        return MAX_SPEED;
    }
}

Implementing an Interface

public class Car implements Vehicle {
    @Override
    public void start() {
        System.out.println("Car started");
    }

    @Override
    public void stop() {
        System.out.println("Car stopped");
    }

    // Can override default method (optional)
    @Override
    public void honk() {
        System.out.println("Car horn: Beep Beep!");
    }
}

New Features in Later Java Versions

Java 8 Additions

  • Default methods: Provide default implementation for methods
  • Static methods: Utility methods related to the interface

Java 9 Additions

  • Private methods: For code reuse within the interface

Multiple Inheritance with Interfaces

interface A {
    void methodA();
}

interface B {
    void methodB();
}

class C implements A, B {
    public void methodA() { /* implementation */ }
    public void methodB() { /* implementation */ }
}

Interface vs Abstract Class

Feature Interface Abstract Class
Methods All abstract (pre-Java 8) Can have concrete methods
Variables Only constants Any variables
Inheritance Multiple Single
Constructors No Yes
Access modifiers Public by default Can be any
State No instance state Can maintain state

Functional Interfaces (Java 8+)

An interface with exactly one abstract method (can have multiple default/static methods):

@FunctionalInterface
interface Calculator {
    int calculate(int a, int b);

    default void printResult(int result) {
        System.out.println("Result: " + result);
    }
}

Interfaces are fundamental to Java's design and are widely used for abstraction, polymorphism, and to define contracts that classes must follow.