⬅ Previous Next ➡

Interfaces & Abstract Classes

Interfaces & Abstract Classes in Java (Multiple Inheritance, Functional Interfaces, Comparison)
  • Interface defines a set of behaviors (method rules) that a class can implement.
  • Interfaces help achieve multiple inheritance in Java.
  • Abstract class is a partially implemented class that can have both abstract and normal methods.

1) Creating Interfaces

  • Use keyword interface.
  • A class implements an interface using implements.
  • Interface methods are public abstract by default.
  • Interface variables are public static final by default (constants).
interface Printable {
    void print(); // public abstract by default
}

class NotesPage implements Printable {
    public void print() {
        System.out.println("Printing Notes Page...");
    }

    public static void main(String[] args) {
        Printable p = new NotesPage();
        p.print();
    }
}

2) Multiple Inheritance Using Interfaces

  • A class can implement more than one interface (comma separated).
  • This is how Java supports multiple inheritance safely.
interface Login {
    boolean check(String email, String pass);
}

interface Profile {
    void showProfile(String name);
}

class UserApp implements Login, Profile {

    public boolean check(String email, String pass) {
        return email.contains("@") && pass.length() >= 4;
    }

    public void showProfile(String name) {
        System.out.println("User: " + name);
    }

    public static void main(String[] args) {
        UserApp u = new UserApp();
        System.out.println("Login: " + u.check("souravshu562@gmail.com", "1234"));
        u.showProfile("Sourav");
    }
}

3) Functional Interfaces

  • Functional Interface has exactly one abstract method.
  • Used with Lambda Expressions (Java 8+).
  • Use @FunctionalInterface to avoid accidental extra abstract methods.
@FunctionalInterface
interface Finder {
    boolean find(String text);
}

class FunctionalInterfaceDemo {
    public static void main(String[] args) {

        // Lambda implementation
        Finder f = (t) -> t.toLowerCase().contains("java");

        System.out.println(f.find("I am learning Java"));
        System.out.println(f.find("Python is also good"));
    }
}

4) Abstract Classes

  • Use keyword abstract.
  • Can contain:
    • Abstract methods (no body)
    • Normal methods (with body)
    • Instance variables
    • Constructor
  • Object cannot be created directly from abstract class.
abstract class Course {
    String courseName;

    Course(String courseName) {
        this.courseName = courseName;
    }

    abstract void start(); // must be implemented

    void info() { // normal method
        System.out.println("Course: " + courseName);
    }
}

class JavaCourse extends Course {
    JavaCourse() {
        super("Java Programming");
    }

    void start() {
        System.out.println("Java course started!");
    }

    public static void main(String[] args) {
        Course c = new JavaCourse();
        c.info();
        c.start();
    }
}

5) Interface vs Abstract Class (Comparison)

  • Purpose:
    • Interface: common behavior contract for unrelated classes.
    • Abstract class: common base with shared code + template.
  • Methods:
    • Interface: mainly abstract methods (also supports default/static methods).
    • Abstract class: abstract + normal methods.
  • Variables:
    • Interface: only constants (public static final).
    • Abstract class: instance variables allowed.
  • Constructor:
    • Interface: not allowed.
    • Abstract class: allowed.
  • Inheritance:
    • Interface: class can implement multiple interfaces.
    • Abstract class: class can extend only one abstract class.
// Quick Rule:
// Use Interface -> when you need multiple inheritance / common contract.
// Use Abstract Class -> when you need shared code + common state (variables).
⬅ Previous Next ➡