0/0
OOP Principles

Principles of OOP: Abstraction

Master the art of hiding complexity — from abstract classes that define incomplete blueprints to interfaces that specify contracts. Learn when to use each, how they enable multiple inheritance, and how Java 8 default methods changed the game.

Explore Topics

🏗
Abstract Classes
Incomplete blueprints that subclasses must complete
🔍 Abstraction Layer Inspector — Abstract Class vs Interface
VIEW:
Feature
Abstract Class
Interface
▶Constructors
✅
❌
▶Instance Fields
✅
❌
▶All Access Modifiers
✅
❌
▶Abstract Methods
✅
✅
▶Concrete Methods
✅
⚠️
▶Static Methods
✅
✅
▶Multiple Inheritance
❌
✅
▶Extend Interfaces
❌
✅
▶Extend Classes
✅
❌
▶Can Be Final
❌
❌
▶Can Instantiate
❌
❌
▶Diamond Problem
❌
⚠️
7
Abstract Class features
6
Interface features (incl. limited)
🏗
Abstract Classes
Incomplete blueprints that subclasses must complete

Abstraction means hiding unnecessary details and exposing only what matters. Think of driving a car — you use the steering wheel and pedals without knowing how the engine works internally. In Java, abstract classes are like blueprints for a house: they define the structure but leave some rooms unfinished. A subclass must complete the work by implementing all abstract methods. An abstract class can contain both abstract methods (no body) and concrete methods (with body). You use the abstract keyword to mark a class and its incomplete methods.

Key Ideas
  • Abstraction = hiding complexity, exposing only what matters
  • Abstract class = incomplete class (like a house blueprint)
  • Abstract method = declaration only, no implementation body
  • Subclass MUST implement all inherited abstract methods
  • Abstract classes CAN have concrete (complete) methods too
  • Marked with the "abstract" keyword on both class and method
  • Used to define a generalized form shared between subclasses
Strengths
  • ✦Defines a common template that all subclasses follow
  • ✦Can share concrete code (DRY — Don't Repeat Yourself)
  • ✦Supports runtime polymorphism through abstract references
  • ✦Fields and constructors allow shared state initialization
Weaknesses
  • △Single inheritance only — a class can extend just one abstract class
  • △Tight coupling — subclass is bound to the abstract class hierarchy
  • △Cannot instantiate directly — requires a concrete subclass
  • △Changes to the abstract class ripple through all subclasses
Used in:java.util.AbstractList provides a skeletal List implementation. Subclasses like ArrayList only override the abstract get() and size() methods while inheriting 20+ concrete methods for free.
Code Snippets

Quiz

Abstract Classes Quiz
QUIZ 1/7
abstract class Animal {
    Animal() { speak(); }
    abstract void speak();
}
class Dog extends Animal {
    String sound = "Woof";
    void speak() { System.out.println(sound); }
}
new Dog();
What is the output of this code?

Diagrams

Abstraction Hierarchy — Classes & Interfaces
Loading diagram...
Java Interface Evolution Timeline
Loading diagram...

Practice

Classify each feature: does it belong to Abstract Classes, Interfaces, or Both?
EXERCISE 1/6
Has constructors
Can have instance fields
Supports multiple inheritance
Can declare abstract methods
Has default methods (Java 8+)
All access modifiers allowed
Variables are static final by default
Cannot be instantiated
Used with extends keyword
Used with implements keyword
Single parent only
Members are public by default
Abstract Class Only
Interface Only
Both
Abstract & Interface Myths
EXERCISE 2/6
An abstract class must have at least one abstract method.
Interface variables are public static final by default.
You can create an object from an interface using the new keyword.
A method can be both abstract and final at the same time.
Interfaces support multiple inheritance in Java.
An abstract class can extend a concrete (non-abstract) class.
Default methods in interfaces can access instance fields of the implementing class.
The overriding method must provide at least as much access as the overridden method.
Abstract Dispatch Trace
EXERCISE 3/6
abstract class Shape { Shape() { describe(); } abstract void describe(); } class Circle extends Shape { String name = "Circle"; void describe() { System.out.println("I am " + name); } } class Main { public static void main(String[] args) { Shape s = new Circle(); s.describe(); } }
Line: new Circle() → constructor chain
Line: s.describe()
Find the Abstract Class Bug
EXERCISE 4/6
Click on the line you think contains the bug, then press Confirm.
1abstract class Printer {
2 public abstract void print();
3}
4class LaserPrinter extends Printer {
5 protected void print() {
6 System.out.println("Laser printing...");
7 }
8}
Complete the Interface Contract
EXERCISE 5/6
Playable { void play(); void pause(); } class MediaPlayer Playable { public void play() { System.out.println("Playing..."); } // pause() is not implemented } class VideoPlayer MediaPlayer { public void pause() { System.out.println("Paused"); } }
Build the Hierarchy — Interface to Concrete
EXERCISE 6/6
Drag the lines into the correct order.
interface Drawable { void draw(); }
abstract class Shape implements Drawable {
protected String color;
Shape(String color) { this.color = color; }
abstract double area();
}
class Circle extends Shape {
Circle(String c, double r) { super(c); this.r = r; }
private double r;
public void draw() { System.out.println("Drawing " + color); }
double area() { return Math.PI * r * r; }
}

References

📋

Abstract Methods and Classes — Oracle Java Tutorials

docs.oracle.com

docs
📋

Defining an Interface — Oracle Java Tutorials

docs.oracle.com

docs
📋

Default Methods — Oracle Java Tutorials

docs.oracle.com

docs
📄

Effective Java Item 20: Prefer Interfaces to Abstract Classes

dev.to

article
📄

Abstract Class vs Interface in Java — Baeldung

baeldung.com

article