Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Object-Oriented Concepts

Abstract classes vs. interfaces in Java

Compare Java abstract classes and interfaces: state, constructors, multiple inheritance, default methods and a practical checklist for choosing.

Both abstract classes and interfaces describe a type that cannot be instantiated on its own and that leaves some methods for others to implement. They overlap more since Java 8 added default methods, but they still differ in one decisive way: a class can extend only one class, yet implement any number of interfaces. And only a class can hold per-object state.

A good rule of thumb: use an interface to describe a capability or a role that unrelated classes can share, and an abstract class to share code and fields among closely related classes.

Java
interface Flyer { void fly(); }                    // a capability
abstract class Vehicle { protected int speed; }    // shared state + code

Try it

Java Interface for the role, abstract class for the shared code

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Multiple interfaces, one superclass

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Comparison

Feature Abstract class Interface
Instance fields Yes, any visibility No; only public static final constants
Constructors Yes (called through super(...)) No
Method bodies Yes default, static and private methods
Abstract methods Optional, any visibility except private Implicitly public abstract
Inheritance A class extends one class A class implements many; an interface extends many
Member visibility private, package, protected, public Methods public; private helpers since Java 9
final methods Yes No
Functional (lambda) use No Yes, if it has one abstract method

Choosing

  • Choose an interface when unrelated classes should be usable through the same API, when you want callers to be able to supply lambdas, or when a class may need several such roles.
  • Choose an abstract class when subclasses share fields, constructors or helper code, or when you need protected access and the template-method pattern.
  • Often you want both: an interface as the public contract plus an abstract base class that implements most of it (the JDK does this with List and AbstractList).
  • Need a closed set of implementations? Use a sealed interface or class.

Usage notes

  • Default methods cannot use object state; they can only call other interface methods. If two interfaces supply the same default, the implementing class must override it.
  • A class method always wins over an interface default with the same signature.
  • Adding a method to a published interface is source compatible only if it is default. Adding an abstract method breaks every implementor.
  • Abstract classes can have a main method and be partially implemented, but they still cannot be created with new (anonymous subclasses aside).
  • Marker interfaces such as Serializable carry no methods and only tag a type.