📘Interfaces in Java

1. What is an Interface?

An interface is a blueprint of a class.

It contains:

  • Method declarations (by default public abstract)
  • Constants (public static final)
  • From Java 8+: default + static methods
  • From Java 9+: private methods (helper methods)

Example:

interface Animal {
    void eat();     // abstract method
}
        

Usage:

class Dog implements Animal {
    public void eat() {
        System.out.println("Dog eats");
    }
}
        

👉 A class implements an interface. 👉 It MUST provide implementations for all abstract methods.


2. Why Interfaces Exist

Java does NOT allow multiple inheritance with classes.

You cannot do this:

class C extends A, B { }  // ❌ illegal
        

Because of the Diamond Problem (ambiguity).

So Java uses interfaces to achieve multiple inheritance safely.


3. Multiple Inheritance Using Interfaces

Example:

interface A {
    void show();
}

interface B {
    void show();
}

class Test implements A, B {
    public void show() {
        System.out.println("Resolved");
    }
}
        

This is VALID.

Why?

Because interfaces don’t carry implementation (mostly). So Java forces YOU to resolve the conflict.

👉 No ambiguity.


4. Diamond Problem Explained

   A
  / \
 B   C
  \ /
   D
        

If A has print() B and C inherit it D inherits B and C.

Which print() should D use?

Java avoids this by not allowing multiple class inheritance.

Interfaces solve it because:

  • Methods are abstract
  • Child must implement explicitly


5. Default Methods Conflict (Java 8+)

If interfaces have default methods:

interface A {
    default void show() {
        System.out.println("A");
    }
}

interface B {
    default void show() {
        System.out.println("B");
    }
}

class Test implements A, B {
    public void show() {
        System.out.println("My logic");
    }
}
        

👉 Compiler forces override.

No silent ambiguity.


6. Key Rules

✔ Class → Class

Single inheritance only

class B extends A
        

✔ Interface → Interface

Multiple allowed

interface C extends A, B
        

✔ Class → Interfaces

Multiple allowed

class Test implements A, B, C
        

❌ Interface → Class

Not allowed.


To view or add a comment, sign in

More articles by Swastik Samal

  • Difference between var, let & const in JS

    In JavaScript, variable declaration is not merely syntactic—it defines scope boundaries, mutability guarantees, and…

  • Static and Non-Static Control Flow

    In Java, program execution follows a deterministic order. When a class contains both static and non-static members, two…

  • Factory Method and Singleton Class

    ✅ Factory Method Pattern Definition The Factory Method Pattern is a creational design pattern that provides an…

  • Constructors in Java

    In Java, a constructor is a special method used to initialize objects when they are created. 🔹 What is a Constructor?…

  • Types of Functions in JS

    1️⃣ Function Declaration Definition: A named function defined using the function keyword. Key Points Fully hoisted Can…

  • 📘Datatypes in JS

    JavaScript has 8 built-in data types, divided into Primitive and Non-Primitive (Reference). ✅ Primitive Data Types…

  • 📘Modifiers in Java

    Modifiers are reserved keywords in Java used to control the accessibility, visibility, and behavior of classes…

  • JS Token

    Tokens are the smallest meaningful units the JS engine understands. Before execution, your code is broken into these…

  • Method Overloading vs Method Overriding vs Method Hiding

    1️⃣ Method Overloading Compile-time polymorphism What it is Same method name, different parameter list, in the same…

  • 📘 Encapsulation & Access Modifiers

    1️⃣ What is Encapsulation? Encapsulation is the process of binding data (variables) and methods together and…

Explore content categories