CodeBlanks Logo Icon CodeBlanks

Abstract vs interface

Learn the difference between abstract class and interface.

Abstract class vs interface

Abstract class and interface provide similar contracts but differ. An abstract class carries state (fields) and concrete methods; a single class extends it. An interface carries no state (except constants); many classes implement it.

Example: abstract class Account { double balance; abstract void deposit(double amount); } is an abstract class; it has a balance field. interface Printable { void print(); } is an interface; it contains no field. A class extends one abstract class but implements many interfaces. Choose abstract class for shared state and common methods; choose interface for a contract only. The right tool depends on the design need.

Abstract for shared state

An abstract class is ideal for carrying state and behavior shared by subclasses. An interface cannot hold state; so when common fields are needed, an abstract class is chosen.

Example: in a bank account hierarchy if the balance field is needed in all subclasses, you write abstract class Account { double balance; }. Subclasses do not redefine this field. Similarly void deposit and void withdraw common methods are gathered in the abstract class. This is not possible with an interface. An abstract class represents an 'is-a' relationship; an interface represents 'can-do'. The right choice keeps the class hierarchy simple.

Practice Define the Account abstract class

Define an abstract class Account. Add a double balance field and a constructor Account(double balance) that assigns the field. Add the abstract void deposit(double amount) signature. Write class SavingsAccount extends Account; its constructor SavingsAccount(double balance) calls the parent with super(balance). Write the public void deposit(double amount) method; its body runs balance += amount; and System.out.println("New balance: " + balance);. In main create SavingsAccount a = new SavingsAccount(100); then call a.deposit(50).

Starter code

Main.java
// Define the abstract Account class
// Add balance field and abstract deposit
// Write the SavingsAccount subclass
// Call deposit in main

Sample solution

Main.java
abstract class Account {
    double balance;
    Account(double balance) {
        this.balance = balance;
    }
    abstract void deposit(double amount);
}
class SavingsAccount extends Account {
    SavingsAccount(double balance) {
        super(balance);
    }
    public void deposit(double amount) {
        balance += amount;
        System.out.println("New balance: " + balance);
    }
}
class Main {
    public static void main(String[] args) {
        SavingsAccount a = new SavingsAccount(100);
        a.deposit(50);
    }
}

Expected output

New balance: 150.0

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the class relationship

Question
class SavingsAccount ____ Account {
    public void deposit(double amount) { }
}
Answer
class SavingsAccount extends Account {
    public void deposit(double amount) { }
}

Explanation: The extends keyword inherits an abstract class.

2 When is an abstract class preferred over an interface?

  • When only a contract is needed
  • When shared state and methods are needed Correct answer
  • Never
  • When multiple inheritance is needed

Explanation: Shared state and methods require an abstract class.

3 Match the abstract class and interface differences

Term Meaning
abstract class Carries state
interface Carries contract
extends Single inheritance
implements Multiple implementation

All lessons in Intermediate