π 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 objectabstract 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
| Feature | Abstract Class | Normal Class |
|---|---|---|
| Object creation | β Not allowed | β Allowed |
| Methods | Abstract + Normal | Only normal |
| Purpose | Blueprint | Full 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