Dung (Donny) Nguyen

Senior Software Engineer

Abstract Class and Interface

Differences

Java 8 (and later)

Feature Abstract Class Interface
Instance Methods Can have fully implemented instance methods Can also have implemented instance methods, but they must bear the keyword default
Static Methods Can have fully implemented static methods Can have fully implemented static methods
Variables Can have instance variables of any kind Can have only public static final variables
Visibility Can have any visibility (public, protected, package-level, private) Only public

Java 7 (and earlier)

Feature Abstract Class Interface
Instance Methods Can have fully implemented instance methods Can not have
Static Methods Can have fully implemented static methods Can not have
Instance Variables Can have instance variables of any kind Can have only public static final variables
Visibility Can have any visibility (public, protected, package-level, private) Only public

Usage

Abstract Class

Interface

Additional Notes

It is possible to put abstract methods into an interface. In fact, that’s one of the primary purposes of interfaces.

We don’t need to use the abstract keyword when declaring methods in an interface, although we can if we want to be explicit.

For example:

public interface MyInterface {
    // Abstract method (no implementation)
    void abstractMethod();
    
    // This is equivalent to the above
    abstract void explicitAbstractMethod();
}

Some IDEs will show the warning message when we add the abstract keyword explicitly:

Modifier 'abstract' is redundant for interface methods