Skip to main content

Command Palette

Search for a command to run...

07. Types of Interface in Java

Published
•3 min read•View as Markdown

Perfect! Let’s dive into Interfaces in Java, a core OOP concept closely tied to abstraction and multiple inheritance. I’ll explain it fully with types, rules, and examples.


Interface in Java


1. What is an Interface?

  • Definition: An interface is a reference type in Java that can contain abstract methods, default methods, static methods, and constants.

  • Purpose:

    1. Defines a contract that implementing classes must follow.

    2. Supports multiple inheritance (a class can implement multiple interfaces).

    3. Provides abstraction without implementation.


2. Syntax of Interface

interface Vehicle {
    int MAX_SPEED = 120; // public, static, final by default
    void start();        // abstract method by default
}

Key Points:

  • All variables are public, static, final by default.

  • All methods are public and abstract by default (except default and static methods).

  • Cannot create objects of an interface directly.


3. Implementing an Interface

interface Vehicle {
    void start();
}

class Car implements Vehicle {
    public void start() {  // must implement start()
        System.out.println("Car started");
    }
}

public class Main {
    public static void main(String[] args) {
        Vehicle v = new Car(); // Interface reference
        v.start();
    }
}

4. Types of Interface Methods

Method TypeDefinitionExample
Abstract MethodMust be implemented by implementing classvoid start();
Default Method (Java 8+)Method with body in interface; can be overriddendefault void fuel() { System.out.println("Fueling"); }
Static Method (Java 8+)Belongs to interface; called via interface namestatic void info() { System.out.println("Info"); }
Private Method (Java 9+)Helper method inside interfaceprivate void check() {}

5. Types of Interfaces

  1. Normal / Regular Interface

    • Contains only abstract methods and constants.
    interface Vehicle {
        void start();
        int MAX_SPEED = 120;
    }
  1. Functional Interface (Java 8+)

    • Contains exactly one abstract method.

    • Can be used with Lambda Expressions.

    @FunctionalInterface
    interface Drawable {
        void draw();
    }
  1. Marker Interface

    • Empty interface, no methods.

    • Used to mark a class with metadata.

    • Examples: Serializable, Cloneable

    interface Marker {} // Marker interface
  1. Private / Default / Static Method Interface

    • Java 8+: Default and static methods allowed.

    • Java 9+: Private methods allowed for helper methods inside interface.


6. Key Rules of Interfaces

  1. Cannot instantiate directly: Vehicle v = new Vehicle(); // ❌

  2. A class can implement multiple interfaces (supports multiple inheritance).

     class Car implements Vehicle, Serializable {}
    
  3. Variables in interface are public, static, final by default.

  4. Methods in interface are public and abstract by default (except default, static, private).

  5. Can extend another interface.

     interface ElectricVehicle extends Vehicle {}
    

7. Advantages of Interfaces

  1. Multiple Inheritance: A class can implement multiple interfaces.

  2. Abstraction: Only defines “what to do”, not “how to do”.

  3. Loose Coupling: Interfaces allow implementation details to change without affecting dependent code.

  4. Polymorphism: Objects can be referenced by interface type.


✅ Summary Table – Interfaces

Type of InterfaceDescriptionExample / Use Case
Normal / RegularAbstract methods + constantsVehicle, Shape
FunctionalExactly 1 abstract method, Lambda-friendlyDrawable, Comparator
MarkerEmpty interface, metadataSerializable, Cloneable
Default / Static / PrivateMethods with body inside interfaceHelper or reusable methods

More from this blog

SS-JAVA

23 posts