Java
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.
Exercise 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).
Show example solution
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);
}
}
Test yourself
-
Complete the class relationship
class SavingsAccount ____ Account {
public void deposit(double amount) { }
}
Show answer
class SavingsAccount extends Account {
public void deposit(double amount) { }
}
-
When is an abstract class preferred over an interface?
- When only a contract is needed
- When shared state and methods are needed
- Never
- When multiple inheritance is needed
Show answer
When shared state and methods are needed — Shared state and methods require an abstract class.
-
Match the abstract class and interface differences
- abstract class
- Carries state
- interface
- Carries contract
- extends
- Single inheritance
- implements
- Multiple implementation
Next lesson: Sealed class