← Back to Courses
© 2025 sampathirao yoganandh
Week 8 · CO-2

Interfaces

Defining, Implementing, Accessing, Extending Interfaces; Default & Static Methods  CO-2

What is an Interface?

An interface is a 100% abstract type (in traditional Java) that defines a contract — a set of methods that a class must implement. Interfaces support:

Interface members (pre-Java 8)Default nature
Variablespublic static final (constants)
Methodspublic abstract

Defining & Implementing

// Defining an interface
interface Drawable {
    double PI = 3.14159;     // implicitly public static final
    void draw();              // implicitly public abstract
    double area();
}

// Implementing the interface
class Circle implements Drawable {
    double r;
    Circle(double r) { this.r = r; }

    @Override
    public void draw()   { System.out.println("Drawing circle"); }

    @Override
    public double area() { return PI * r * r; }
}

// Implementing multiple interfaces
interface Resizable { void resize(double factor); }

class Square implements Drawable, Resizable {
    double side;
    @Override public void   draw()              { System.out.println("Drawing square"); }
    @Override public double area()              { return side * side; }
    @Override public void   resize(double f)   { side *= f; }
}

Interface Reference

Just like abstract classes, an interface reference can hold any object that implements it.

Drawable d;

d = new Circle(5.0);
d.draw();                         // Circle's draw
System.out.println(d.area());    // Circle's area — runtime dispatch

d = new Square();
d.draw();                         // Square's draw
📌 Key PointInterface references enable runtime polymorphism and are the foundation of programming to abstractions (Dependency Inversion Principle).

Extending Interfaces

An interface can extend one or more other interfaces using extends.

interface Shape   { double area(); }
interface Shape3D extends Shape {
    double volume();
}

// extending multiple interfaces
interface Printable  { void print(); }
interface Saveable   { void save(); }
interface Document extends Printable, Saveable {
    void open();
}

Default & Static Methods (Java 8+)

Java 8 added default and static methods to interfaces, allowing backward-compatible evolution of APIs.

TypeKeywordCalled viaCan be overridden
Default methoddefaultObject referenceYes (in implementing class)
Static methodstaticInterfaceName.method()No
interface Logger {
    void log(String msg);   // abstract — must implement

    default void logInfo(String msg) {   // default — optional to override
        log("[INFO] " + msg);
    }

    static Logger console() {             // static factory method
        return msg -> System.out.println(msg);
    }
}

Abstract Class vs Interface

FeatureAbstract ClassInterface
Keywordabstract classinterface
Multiple inheritance❌ No✅ Yes
Constructors✅ Yes❌ No
FieldsAny typepublic static final only
MethodsAny (abstract + concrete)abstract, default, static
Access modifiersAnyAll public by default
Use whenShared base code + partial abstractionFull contract; multiple inheritance
📌 Key PointRule of thumb: Use an interface when you need to define a contract (what to do). Use an abstract class when you need to share code (how to partly do it).
📄

Download PDF Notes

Save these notes as a PDF for offline reference

View / Download PDF