Interfaces & Abstract Classes

    Atul Kabra3 min readUpdated

    An interface is a contract: a list of methods a class promises to provide, with no implementation of its own (by default). An abstract class is a partial blueprint: it can have some implemented methods and some unimplemented (abstract) ones, and it cannot be instantiated directly. Both let you program to an abstraction, but they solve slightly different problems. A class implements interfaces and extends an abstract class.

    Defining and implementing an interface

    // A contract: anything Drawable can be drawn.
    interface Drawable {
        void draw(); // no body — implementers must supply one
    }
    
    class Circle implements Drawable {
        @Override
        public void draw() {                 // fulfils the contract
            System.out.println("Drawing a circle");
        }
    }
    

    A class can implement many interfaces, separated by commas — this is how Java achieves the flexibility of multiple inheritance without its ambiguity.

    interface Drawable { void draw(); }
    interface Clickable { void click(); }
    
    // A Button promises both behaviours.
    class Button implements Drawable, Clickable {
        public void draw()  { System.out.println("Drawing button"); }
        public void click() { System.out.println("Clicked"); }
    }
    

    Default methods

    Since JDK 8, an interface can provide a default method with a body, so adding a method to an interface does not break every existing implementer.

    interface Greeter {
        String name();
        default void greet() {               // has an implementation
            System.out.println("Hello, " + name());
        }
    }
    // Implementers get greet() for free but must supply name().
    

    Abstract classes

    An abstract class sits between an interface and a concrete class. It can hold fields, constructors, and both abstract and concrete methods. Subclasses must implement the abstract methods.

    abstract class Shape {
        String label;                        // state — interfaces cannot hold instance state
    
        Shape(String label) { this.label = label; } // a constructor
    
        abstract double area();              // must be implemented by subclasses
    
        void describe() {                    // shared concrete behaviour
            System.out.println(label + " has area " + area());
        }
    }
    
    class Square extends Shape {
        double side;
        Square(double side) { super("Square"); this.side = side; }
        @Override
        double area() { return side * side; }
    }
    

    You cannot write new Shape(...) — only concrete subclasses can be instantiated.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Interface vs abstract class — which to use?

    InterfaceAbstract class
    Multiple inheritanceA class can implement manyA class extends only one
    State (instance fields)No instance fieldsCan have fields and constructors
    Method bodiesdefault/static onlyAny concrete methods allowed
    Best forA capability several unrelated types shareA common base with shared state/code

    Rule of thumb: use an interface to express a capability ("can be drawn", "can be compared") that unrelated classes might share; use an abstract class when several closely related classes share state and partial implementation.

    Common mistakes

    • Forgetting to make implementing methods public. Interface methods are implicitly public, so the implementation must be public too.
    • Trying to instantiate an abstract class or interface. You must instantiate a concrete subclass/implementer.
    • Putting instance state in an interface. Interface fields are implicitly public static final constants, not per-object state.
    • Extending multiple classes. Java forbids it; implement multiple interfaces instead.
    • Overusing default methods. They are a compatibility tool, not a place to hide large logic.

    FAQ

    Can an interface have constants? Yes — any field in an interface is automatically public static final.

    Can an abstract class have no abstract methods? Yes, though that is unusual; it simply means the class is incomplete-by-design and cannot be instantiated.

    Keep going

    Next, organise code with Packages & Access Modifiers, or revisit Polymorphism in Java. All tutorials are on the Java hub; for guided practice in Jalgaon, see the Java course.


    Design clean abstractions at Infoplanet, Jalgaon. Join the waitlist for the Java course.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides