Skip to content
QuizMaker logoQuizMaker
Activity
Java Programming: From Zero to Enterprise
2. Object-Oriented Programming
1. Getting Started with Java & the JVM
2. Data Types & Variables
3. Control Flow: Ifs & Loops
4. String Manipulation in Depth
5. Methods (Functions) Architecture
6. Arrays & The Enhanced For Loop
7. User Input via Scanner
8. Mathematical Operations & The Math Class
9. Operators in Depth
10. Block Scope & Variable Lifecycles
11. Introduction to Object-Oriented Programming
12. Classes & Instances Deep Dive
13. Constructors
14. Encapsulation & The 'this' Keyword
15. Inheritance: Extending Functionality
16. Polymorphism & Method Overriding
17. Abstraction & Abstract Classes
18. Interfaces: The Ultimate Contract
19. Packages & Access Modifiers
20. Enums (Enumerations)
21. Exceptions: Handling Runtime Errors
22. The 'throw' and 'throws' keywords
23. Dates, Times, and Formatting
24. Enumerable Data Structures
25. LinkedLists: The Alternative
26. HashMaps: Key-Value Architecture
27. HashSets: The Art of Uniqueness
28. Iterator: Safe Collection Traversal
29. Wrapper Classes & Autoboxing
30. Basic File I/O
31. Generics: Type-Safe Templates
32. Lambda Expressions & Functional Interfaces
33. The Stream API: Functional Data Pipelines
34. Optional: Beating the NullPointerException
35. Multithreading & Concurrency Basics
36. JDBC: Connecting to SQL Databases
37. Annotations & Reflection
38. The JVM Garbage Collector
39. Introduction to Spring Boot
40. Unit Testing with JUnit
41. Java Collections for DSA
CONTENTS

17. Abstraction & Abstract Classes

The fourth pillar of OOP. Hiding complex implementation details using abstract blueprints.

Java Programming: From Zero to Enterprise
2. Object-Oriented Programming
February 22, 2026
63
A

[!NOTE] The final pillar of Object-Oriented Programming is Abstraction. It focuses on hiding complex implementation details from the user, leaving only the high-level essential features visible.

In Java, we achieve the strictest form of Abstraction using the abstract keyword.

The Problem with Generic Classes

Consider the Animal parent class from the previous chapter.

Animal mysteriousThing = new Animal();
mysteriousThing.makeSound(); // ???

Conceptually, a "pure generic animal" does not exist in the real world. You can have a Dog or a Cat, but you cannot literally spawn an "Animal". It's too vague.

Furthermore, we know every single animal makes a sound, but what sound does a generic Animal make? We don't know yet! We need a way to force all children to implement a makeSound() method, without us having to guess what it is in the Parent.

The Abstract Class

By applying the abstract keyword to a class, you permanently restrict it. An abstract class cannot be instantiated using the new keyword. It exists solely to be inherited by child classes.

Furthermore, inside an abstract class, you can declare Abstract Methods. These are methods that have no body (no curly braces). They act as a strict contract: any child class that inherits this parent must write the code for these methods, or the child will refuse to compile!

// 1. We lock the class so nobody can do 'new Animal()' anymore
abstract class Animal {
    String name;
    
    // Regular methods are still totally fine here!
    public void sleep() {
        System.out.println("Zzz...");
    }

    // 2. ABSTRACT METHOD! No body. It forces children to implement it.
    public abstract void makeSound();
}

// 3. The Child Class
class Pig extends Animal {
    // If the Pig class doesn't override makeSound, the Java Compiler will throw a massive error!
    @Override
    public void makeSound() {
        System.out.println("Oink oink!");
    }
}

public class Main {
    public static void main(String[] args) {
        Pig myPig = new Pig(); // Totally fine
        myPig.makeSound();
        myPig.sleep(); // Inherited regular method
        
        // Animal a = new Animal(); // ERROR: Cannot instantiate abstract class!
    }
}

Summary of Rules

  1. Abstract classes can have both abstract methods and regular methods.
  2. An abstract method can ONLY exist inside an abstract class.
  3. You cannot create objects from an abstract class. It is a strictly parent-level blueprint template.

Abstraction Lets You Define What Must Exist

An abstract class is useful when several related classes share some implementation but still must provide their own specific behavior. It gives you a partly built parent with some required blanks for children to fill.

Template Method Example

abstract class ReportGenerator {
    public void generate() {
        fetchData();
        formatReport();
        export();
    }

    void fetchData() {
        System.out.println("Fetching data");
    }

    abstract void formatReport();
    abstract void export();
}

The generate() flow is shared, but each report type can decide its own formatting and export logic.

When Abstract Classes Fit

  • You want to share fields or helper methods across related children.
  • You want to prevent creation of a vague parent object.
  • You want to force child classes to implement specific methods.

Common Mistakes

  • Creating abstract classes when an interface would be enough.
  • Putting too much logic in the abstract parent and making children hard to customize.
  • Forgetting that a child must implement inherited abstract methods unless the child is also abstract.
  • Trying to instantiate an abstract class directly.

Mini Practice

Create an abstract class PaymentProcessor with an abstract pay() method and a normal printReceipt() method. Implement CardPayment and UpiPayment.

Practice Lab: Payment Processor Template

Create a partly shared parent class with required child behavior.

  1. Create abstract class PaymentProcessor.
  2. Add normal method printReceipt().
  3. Add abstract method pay(double amount).
  4. Create CardPayment and UpiPayment child classes.
  5. Try to instantiate PaymentProcessor directly and observe the compile error.

Goal: Understand when a parent is a template, not a real object.

Revision Checkpoint

  • Abstract class: Cannot be instantiated directly.
  • Abstract method: Declares required behavior without a body.
  • Shared logic: Abstract classes can also contain normal methods.
  • Child responsibility: Concrete child classes must implement abstract methods.
  • Use case: Shared template for related classes.

Before the quiz: Identify what belongs in the abstract parent and what belongs in children.

Share this article

Share on TwitterShare on LinkedInShare on FacebookShare on WhatsAppShare on Email

Test your knowledge

Take a quick quiz based on this chapter.

mediumJava
Quiz: Abstraction
10 questions5 min
Lesson 7 of 10 in 2. Object-Oriented Programming
Previous in 2. Object-Oriented Programming
16. Polymorphism & Method Overriding
Next in 2. Object-Oriented Programming
18. Interfaces: The Ultimate Contract
Back to Java Programming: From Zero to Enterprise
Back to moduleCategories