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.

Feb 22, 20263 views0 likes0 fires
18px

[!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.

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
2 questions5 min

Continue Learning

18. Interfaces: The Ultimate Contract

Intermediate
14 min

19. Packages & Access Modifiers

Beginner
8 min

20. Enums (Enumerations)

Intermediate
7 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 Java Programming: From Zero to EnterpriseAll Categories