Advanced OOP

Interface vs Abstract Class

Interface vs Abstract Class

Both are used to achieve abstraction in Java. Neither can be instantiated, and both can contain methods that child classes must implement. The difference lies in what they represent and what they can hold.

In simple words:

Abstract Class = partial blueprint (shared state + code). Interface = contract (what a class must do).

Real-Life Example

text
1Abstract Class → "Vehicle"
2 Every vehicle has wheels, fuel and speed (shared state and code),
3 but each vehicle starts differently.
4
5Interface → "Chargeable"
6 Phone, Laptop and Car can all be charged.
7 They are unrelated, but share the same capability.

Abstract Class Example

abstract class Vehicle {
String name;
int speed;
Vehicle(String name) {
this.name = name;
}
// Abstract method: child must implement
abstract void start();
// Normal method: shared by all vehicles
void stop() {
System.out.println(name + " stopped");
}
}
class Car extends Vehicle {
Car(String name) {
super(name);
}
@Override
void start() {
System.out.println(name + " starts with a key");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v = new Car("Swift");
v.start();
v.stop();
}
}
// Output:
// Swift starts with a key
// Swift stopped

Interface Example

interface Chargeable {
void charge(); // public abstract by default
}
class Phone implements Chargeable {
@Override
public void charge() {
System.out.println("Phone is charging");
}
}
class ElectricCar implements Chargeable {
@Override
public void charge() {
System.out.println("Car is charging at the station");
}
}
public class Main {
public static void main(String[] args) {
Chargeable c1 = new Phone();
Chargeable c2 = new ElectricCar();
c1.charge();
c2.charge();
}
}
// Output:
// Phone is charging
// Car is charging at the station

Main Differences

FeatureAbstract ClassInterface
Keywordabstract classinterface
Used withextendsimplements
MethodsAbstract and normalAbstract, default, static, private
VariablesAny type (instance, static, final)Only public static final constants
ConstructorsYesNo
Instance state (fields)YesNo
Access modifiersAny (private, protected, public)Methods are public (private allowed since Java 9)
Multiple inheritanceA class can extend only oneA class can implement many
Can extend / implementExtends one class, implements many interfacesExtends many interfaces
RelationshipClosely related classes (IS-A)Unrelated classes sharing a capability (CAN-DO)
Speed of changeAdding a method is easy for childrenAdding an abstract method breaks all implementers

Interface with default Method (Java 8+)

Interfaces can provide a method body using default, so new methods can be added without breaking existing classes.

interface Chargeable {
void charge();
default void showStatus() {
System.out.println("Charging status: OK");
}
}

Using Both Together

A class can extend one abstract class and implement many interfaces.

abstract class Vehicle {
abstract void start();
}
interface Chargeable {
void charge();
}
class ElectricCar extends Vehicle implements Chargeable {
@Override
void start() {
System.out.println("Electric car starts silently");
}
@Override
public void charge() {
System.out.println("Electric car is charging");
}
}

When to Use Which?

text
1Classes are closely related and share code or state? → Abstract Class
2 Example: Vehicle → Car, Bike, Truck
3
4Unrelated classes need the same capability? → Interface
5 Example: Chargeable → Phone, Laptop, ElectricCar
6
7Need multiple inheritance of type? → Interface
8
9Need constructors or instance variables? → Abstract Class
Choose Abstract Class when...Choose Interface when...
You want to share common code and fieldsYou want to define a contract only
Child classes are closely relatedClasses are unrelated
You need non-public members or constructorsYou need a class to have multiple "types"
You expect to add non-abstract methods laterYou want loose coupling and easy swapping

Interview Definition

An abstract class is a partially implemented class that can hold state and constructors and is extended by closely related classes. An interface is a contract that defines what a class must do, and a class can implement many of them.

Remember

Abstract class = "is a" with shared state and code. Interface = "can do" capability with no state.