codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🧩

OOP Concepts

23 chapters

1Procedural vs Object-Oriented2Classes, Objects, & Instantiation3Constructors & Destructors4Static Members & Methods5Encapsulation & Access Modifiers6Data Abstraction7Inheritance Types (Single, Multiple)8Compile-Time Polymorphism (Overloading)9Polymorphism & Interfaces10Run-Time Polymorphism (Overriding)11Virtual Functions & V-Tables12Interfaces & Abstract Classes13Generic Programming (Templates & Generics)14Exception Handling in OOP15SOLID Design Principles16Composition over Inheritance17Coupling & Cohesion18UML Diagrams Basics19Creational Patterns (Singleton, Factory)20Structural Patterns (Adapter, Decorator)21Behavioral Patterns (Observer, Strategy)22MVC Architecture Pattern23Object Serialization & Cloning
SubjectsOOP Concepts

Interfaces & Abstract Classes

Updated 2026-04-26
2 min read

Interfaces & Abstract Classes

While Abstract Classes provide a partially implemented blueprint (with both abstract and concrete methods), Interfaces define a purely behavioral contract with zero implementation. Understanding when to use each is a critical design decision in professional OOP development.

1. What is an Interface?

An Interface in Java is a reference type, similar to a class, that can contain only abstract methods (prior to Java 8) and constants. It specifies what a class must do, but not how it does it.

A class that implements an interface must provide concrete implementations for ALL methods declared in the interface.

interface Flyable {
    void takeOff();
    void fly(int altitude);
    void land();
}

class Airplane implements Flyable {
    public void takeOff() { System.out.println("Runway acceleration"); }
    public void fly(int altitude) { System.out.println("Cruising at " + altitude + " feet"); }
    public void land() { System.out.println("Landing gear deployed"); }
}

class Bird implements Flyable {
    public void takeOff() { System.out.println("Flapping wings"); }
    public void fly(int altitude) { System.out.println("Soaring at " + altitude + " feet"); }
    public void land() { System.out.println("Perching on branch"); }
}

Both Airplane and Bird are completely unrelated classes, but they both fulfill the Flyable contract.

2. Java 8+ Interface Evolution

Starting with Java 8, interfaces gained new capabilities:

  • Default Methods: Methods with a body (using the default keyword). This allows adding new methods to existing interfaces without breaking all implementing classes.
  • Static Methods: Utility methods that belong to the interface itself.
interface Loggable {
    void log(String message);

    // Default method: implementing classes get this for free
    default void logError(String message) {
        log("ERROR: " + message);
    }
}

3. Solving the Diamond Problem

Recall that Java bans multiple inheritance of classes because of the Diamond Problem. However, Java allows a class to implement multiple interfaces simultaneously. This provides the benefits of multiple inheritance without the ambiguity.

interface Swimmable {
    void swim();
}

interface Flyable {
    void fly();
}

// A Duck can both swim AND fly
class Duck implements Swimmable, Flyable {
    public void swim() { System.out.println("Paddling"); }
    public void fly() { System.out.println("Flapping"); }
}

4. Abstract Class vs. Interface

FeatureAbstract ClassInterface
MethodsCan have abstract AND concrete methodsAll methods are abstract (pre-Java 8)
VariablesCan have instance variables of any typeCan only have public static final constants
ConstructorsCan have constructorsCannot have constructors
InheritanceA class can extend only ONE abstract classA class can implement MULTIPLE interfaces
Access ModifiersMethods can have any access modifierMethods are implicitly public
Use Case"IS-A" relationship (Dog IS-A Animal)"CAN-DO" relationship (Duck CAN Fly, CAN Swim)

When to use which?

  • Use an Abstract Class when you want to share code (fields, concrete methods) among several closely related classes.
  • Use an Interface when you want to define a capability that can be shared by unrelated classes (like Serializable, Comparable, Runnable).


PreviousVirtual Functions & V-TablesNextGeneric Programming (Templates & Generics)

Recommended Gear

Virtual Functions & V-TablesGeneric Programming (Templates & Generics)