OOP · Advanced OOP
Abstract Class
Provides common functionality to all Java objects.
Advantages of the Object Class
- Provides common functionality to all Java objects.
- Reduces duplicate code.
- Simplifies object comparison.
- Supports hashing for collections.
- Makes object printing more readable.
- Enables object cloning when required.
Real-World Problem
Imagine you're developing a Vehicle Management System. Every vehicle has common operations such as Start, Stop, and Display Details. However, the way these operations are performed differs for a Car, Bike, or Bus. Creating a generic Vehicle object doesn't make sense because a vehicle is only a blueprint representing common characteristics.
Java provides Abstract Classes to solve this problem by allowing a common base while forcing child classes to implement specific behaviors. Why Do We Need an Abstract Class?
Many classes share common properties and behaviors but should not be instantiated directly. Without an abstract class, developers often duplicate code across multiple classes, making applications harder to maintain.
An abstract class provides a common implementation while allowing child classes to define their own specific functionality. This improves code reusability, maintainability, and consistency.

What is an Abstract Class?
An Abstract Class is a class declared using the abstract keyword. It cannot be instantiated directly because it represents an incomplete object. An abstract class may contain abstract methods (without implementation) as well as concrete methods (with implementation), enabling child classes to inherit common functionality while implementing their own behavior.

Program 1 – Basic Abstract Class
Program 2 – Abstract vs Concrete Method

Program 3 – Constructor in Abstract Class


Characteristics
- Declared using the abstract keyword.
- Cannot be instantiated.
- Can contain abstract and concrete methods.
- Can have constructors and variables.
- Supports inheritance.
- Achieves partial abstraction.
Advantages
- Promotes code reuse.
- Reduces duplicate code.
- Improves maintainability.
- Provides a common base for related classes.
- Supports partial abstraction.

An abstract class shares common code but forces subclasses to fill in the missing pieces.
abstract class Payment {
abstract void authorize(); // no body — subclass must implement
// Shared template that reuses the abstract step
void process() {
authorize();
System.out.println("Payment complete");
}
}
class CreditCardPayment extends Payment {
void authorize() { System.out.println("Authorizing card..."); }
}
// new Payment() is illegal; new CreditCardPayment() is fine.