7-🧩 Abstract Classes in Java – Complete Guide with Examples, Rules & Common Mistakes

🌟 Introduction

As you progress in Java and Object-Oriented Programming (OOP), you will often face situations where:

  • Some methods should be common for all classes
  • Some methods should be implemented differently by each class
  • And some classes should not be instantiated directly

This is where Abstract Classes come into play.

Abstract classes help in designing programs where you want to define a base structure, but leave some parts incomplete so that child classes can complete them.

πŸ‘‰ They act as a blueprint for other classes.


🧠 What is an Abstract Class?

An abstract class is:

A class that cannot be instantiated and may contain abstract methods (methods without body).


πŸ”‘ Key Idea

πŸ‘‰ Abstract class = incomplete class
πŸ‘‰ Abstract method = method without implementation


🧾 Syntax of Abstract Class

abstract class Animal {

    abstract void sound();

}

πŸ”Ž Explanation

  • abstract class Animal β†’ cannot create object
  • abstract void sound() β†’ method has no body

πŸ’» Basic Example of Abstract Class

abstract class Animal {

    abstract void sound();

}

class Dog extends Animal {

    void sound() {
        System.out.println("Dog barks");
    }

}

public class Main {

    public static void main(String[] args) {

        Dog d = new Dog();
        d.sound();

    }

}

πŸ”Ž Line-by-Line Explanation

abstract class Animal

πŸ‘‰ Parent class is abstract
πŸ‘‰ Cannot create object of Animal


abstract void sound();

πŸ‘‰ Abstract method
πŸ‘‰ No body (no {})


class Dog extends Animal

πŸ‘‰ Dog inherits Animal


void sound()

πŸ‘‰ Dog must implement this method


Dog d = new Dog();

πŸ‘‰ Object created for child class


d.sound();

πŸ‘‰ Calls implemented method


🧾 Output

Dog barks

⚠️ Important Rule

πŸ‘‰ If a class contains an abstract method:

βœ” The class must be declared abstract


🚫 Cannot Create Object of Abstract Class

Animal a = new Animal(); // ❌ Error

πŸ‘‰ Abstract classes cannot be instantiated


🧠 Abstract Class Can Have Normal Methods

abstract class Animal {

    void eat() {
        System.out.println("Animal eats");
    }

    abstract void sound();

}

πŸ”Ž Explanation

πŸ‘‰ Abstract class can have:

βœ” Abstract methods
βœ” Normal methods


πŸ’» Example with Both Methods

abstract class Animal {

    void eat() {
        System.out.println("Animal eats");
    }

    abstract void sound();

}

class Dog extends Animal {

    void sound() {
        System.out.println("Dog barks");
    }

}

public class Main {

    public static void main(String[] args) {

        Dog d = new Dog();

        d.eat();
        d.sound();

    }

}

🧾 Output

Animal eats
Dog barks

πŸ”₯ Abstract Class with Constructors

Yes, abstract classes can have constructors.


πŸ’» Example

abstract class Animal {

    Animal() {
        System.out.println("Animal Constructor");
    }

}

class Dog extends Animal {

    Dog() {
        System.out.println("Dog Constructor");
    }

}

public class Main {

    public static void main(String[] args) {

        Dog d = new Dog();

    }

}

🧾 Output

Animal Constructor
Dog Constructor

🧠 Concept

πŸ‘‰ Constructor is used to initialize parent part
πŸ‘‰ Even abstract class constructor is called


🎯 Real-World Example

abstract class Employee {

    abstract void calculateSalary();

}

class Manager extends Employee {

    void calculateSalary() {
        System.out.println("Manager Salary");
    }

}

class Developer extends Employee {

    void calculateSalary() {
        System.out.println("Developer Salary");
    }

}

🧠 Concept

πŸ‘‰ Employee defines structure
πŸ‘‰ Manager & Developer define behavior


βš™οΈ Rules of Abstract Classes


βœ” Rule 1: Cannot Create Object

abstract class A { }
A obj = new A(); // ❌

βœ” Rule 2: Must Implement Abstract Methods

class Dog extends Animal

πŸ‘‰ Must implement all abstract methods


βœ” Rule 3: Can Have Constructors

βœ” Used during object creation


βœ” Rule 4: Can Have Variables & Methods

βœ” Not limited to abstract methods


πŸ”„ Abstract Class vs Normal Class

FeatureAbstract ClassNormal Class
Object creation❌ Not allowedβœ” Allowed
MethodsAbstract + NormalOnly normal
PurposeBlueprintFull implementation

⚠️ Common Mistakes & Confusions


❌ Mistake 1: Trying to Create Object

Animal a = new Animal(); // ❌

❌ Mistake 2: Not Implementing Abstract Method

class Dog extends Animal {
// missing sound() ❌
}

πŸ‘‰ Compile-time error


❌ Mistake 3: Forgetting abstract keyword

class Animal {
abstract void sound(); // ❌ Error
}

❌ Mistake 4: Thinking Abstract Class Cannot Have Methods

❌ Wrong
βœ” It can have both types


🧨 Tricky Interview Question

abstract class A {

    abstract void show();

}

class B extends A {

    void show() {
        System.out.println("B");
    }

}

πŸ‘‰ Valid or not?

βœ” Valid


🧠 Key Points to Remember

βœ” Abstract class cannot be instantiated
βœ” Used as base class
βœ” Can contain abstract + normal methods
βœ” Child class must implement abstract methods
βœ” Supports constructors


🎯 When to Use Abstract Classes?

Use abstract classes when:

βœ” You want a common base class
βœ” Some methods should be defined later
βœ” You want partial abstraction


🏁 Conclusion

Abstract classes are a powerful feature in Java that help developers design flexible and reusable code structures.

They allow you to:

  • Define a common structure
  • Force child classes to implement important methods
  • Combine abstraction with inheritance

In this article, you learned:

  • What abstract classes are
  • How they work
  • Syntax and examples
  • Rules and limitations
  • Common mistakes

Understanding abstract classes is essential because they are widely used in:

  • Frameworks
  • APIs
  • Large-scale applications

πŸš€ What’s Next?

The next topic is:

πŸ‘‰ Interfaces in Java

Where you will learn:

  • 100% abstraction
  • Multiple inheritance
  • Real-world design patterns

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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