MediumConcept

What is the difference between an abstract class and an interface in Java?

Technology:
Java
Experience:
Junior,
Mid-level

Quick answer

An abstract class can have instance fields, constructors and a mix of implemented and abstract methods but supports only single inheritance, while an interface defines a contract that a class can implement multiple times and, since Java 8, can include default methods.

Detailed explanation

Choose an abstract class when subclasses share state and base behaviour (for example a BaseRepository with a connection field). Choose an interface when you want to define a capability that unrelated classes can have, such as Comparable or Runnable, and when you want callers to depend on an abstraction.

Interface fields are implicitly public static final. Since Java 8 an interface can have default and static methods, and since Java 9 private methods, which lets library authors add methods without breaking existing implementations. A class can implement many interfaces but extend only one class.

Example

java
interface Payment {
    void pay(double amount);
    default String currency() { return "USD"; }
}

abstract class BasePayment implements Payment {
    protected final String accountId;
    BasePayment(String accountId) { this.accountId = accountId; }
}
  • JavaEasy

    What is the difference between == and equals() in Java?

    == compares primitives by value and objects by reference (whether they are the exact same object), while equals() compares the logical content of two objects when the class overrides it.

    Intern 路 Junior 路 Programming Fundamentals
  • JavaEasy

    What is the difference between JDK, JRE and JVM in Java?

    The JVM runs Java bytecode, the JRE is the JVM plus the standard libraries needed to run Java programs, and the JDK is the JRE plus the development tools such as the compiler (javac).

    Intern 路 Junior 路 Programming Fundamentals
  • JavaEasy

    What is the difference between final, finally and finalize in Java?

    final is a keyword that prevents reassigning a variable, overriding a method or extending a class; finally is a block that always runs after try/catch for cleanup; finalize() was a method called by the garbage collector and is now deprecated.

    Intern 路 Junior 路 Programming Fundamentals
  • JavaMedium

    What is the difference between ArrayList and LinkedList in Java?

    ArrayList is backed by a resizable array with O(1) index access and O(n) insertion in the middle, while LinkedList is a doubly linked list with O(n) index access and O(1) insertion once you hold the node; in practice ArrayList is faster for most workloads.

    Junior 路 Mid-level 路 Data Structures
  • JavaMedium

    What is the difference between checked and unchecked exceptions in Java?

    Checked exceptions (subclasses of Exception other than RuntimeException) must be caught or declared with throws, while unchecked exceptions (RuntimeException and Error) do not have to be handled and usually signal programming bugs.

    Junior 路 Mid-level 路 Programming Fundamentals