Chapter 13: Abstraction

Abstraction is all about hiding the complex implementation details and showing only the essential features to the user. It’s like using a TV remote: you press “Power” or “Volume Up” without knowing how the circuits inside actually work — that’s abstraction in real life!

We’ll cover the two main ways Java provides abstraction:

  1. Abstract Classes
  2. Interfaces (with special focus on default methods and multiple inheritance)

We’ll go super slowly, with lots of real-life analogies, complete runnable programs, step-by-step explanations, tables, common mistakes with fixes, and tons of examples you can copy-paste and run right now.

Let’s dive in!

1. What is Abstraction? (The Big Idea)

Abstraction means:

  • Showing only what is necessary
  • Hiding how it is implemented

In Java, we achieve abstraction using:

  • Abstract classes → partial abstraction (some methods implemented, some left abstract)
  • Interfaces → full abstraction (until Java 8 — now they can have default methods)

Real-life analogy:

  • A remote control is an interface — it has buttons like power(), volumeUp(), but you don’t know how the TV actually turns on or changes volume.
  • A smartphone is an abstract class — it has some common features (call, message) already implemented, but some features (camera quality, battery life) are left for specific models (Samsung, iPhone) to define.

2. Abstract Classes

An abstract class:

  • Cannot be instantiated (you can’t do new AbstractClass())
  • Can have both abstract methods (no body) and concrete methods (with body)
  • Can have fields, constructors, final/static methods
  • Used when classes are related and share some common code

Syntax:

Java

Example 1: Shape Hierarchy using Abstract Class

Java

Test it:

Java

Output:

text

3. Interfaces (Full Abstraction + Multiple Inheritance)

An interface is a 100% abstract contract (until Java 8). It defines what a class must do, but not how.

Key Features (Java 8+):

  • All methods are abstract by default (no need to write abstract)
  • Can have default methods (with body — backward compatible)
  • Can have static methods
  • Supports multiple inheritance (a class can implement many interfaces)

Syntax:

Java

Example 2: Payment System using Interface

Java

Test it:

Java

Output:

text

4. Multiple Inheritance using Interfaces

A class can implement multiple interfaces — this is how Java achieves multiple inheritance safely.

Example:

Java

Test:

Java

5. Quick Recap Table (Your Cheat Sheet)

Feature Abstract Class Interface
Instantiation No No
Methods Abstract + Concrete Abstract (default) + Default + Static
Fields Can have instance/static/final Only public static final (constants)
Constructors Yes No
Inheritance Single (extends) Multiple (implements)
Best For Related classes with shared code Unrelated classes with common behavior
Java 8+ Default Methods No need Yes — provides backward compatibility

6. Common Mistakes & Fixes

Mistake Problem Fix
new Shape() Cannot instantiate abstract class Use concrete child: new Circle()
Forgetting to implement abstract method Compile error Implement all abstract methods
default method conflict in multiple interfaces Compile error if same signature Override in implementing class
Trying extends multiple classes Compile error Use implements for interfaces
Abstract method with body Compile error Remove body or make it concrete

7. Homework for You (Practice to Master!)

  1. Basic: Create abstract class Vehicle with abstract start() and concrete stop(). Create Car and Bike implementing it.
  2. Medium: Create interface Resizable with resize(double factor). Make Circle and Rectangle implement it.
  3. Advanced: Create two interfaces Printable and Scannable. Make a PrinterScanner class implement both.
  4. Fun: Create interface Drawable with default draw() that prints “Drawing…”. Implement in Circle, Square.
  5. Challenge: Fix this code:
    Java

You’re doing amazing! Abstraction is what makes large systems clean, maintainable, and scalable — now you can design beautiful class hierarchies.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *