In this tutorial, you’ll learn everything about Abstract Classes and how Polymorphism works together with Abstraction in Java.

We’ll cover what abstract classes are, how they differ from interfaces, and how abstraction and polymorphism work together in real-world applications — along with scenario-based interview questions and practical coding examples.


1. What is an Abstract Class in Java?

An abstract class in Java is a partially implemented class that acts as a blueprint for other classes. It can have both abstract methods (methods without body) and concrete methods (methods with implementation).

It cannot be instantiated directly — meaning you cannot create an object of an abstract class.


abstract class Vehicle {
    abstract void start(); // abstract method (no body)

    void stop() {          // concrete method (has body)
        System.out.println("Vehicle stopped.");
    }
}
  • abstract keyword is used to declare an abstract class or method.
  • Abstract methods have no body and must be implemented by subclasses.
  • Abstract classes can contain constructors, fields, and even static methods.

2. Abstraction and Polymorphism Together

Abstraction means hiding the internal details and showing only what’s necessary. For example — when you drive a car, you press the start button, but you don’t need to know how the engine internally works.

Polymorphism means “many forms” — the same method behaves differently depending on the object calling it.

When combined — Abstraction defines what must be done, and Polymorphism decides how it’s done by each subclass.


3. Real-World Example — Vehicle System


abstract class Vehicle {
    abstract void start(); // abstract method
}

class Car extends Vehicle {
    @Override
    void start() {
        System.out.println("Car starts with a key.");
    }
}

class ElectricCar extends Vehicle {
    @Override
    void start() {
        System.out.println("Electric Car starts with a button.");
    }
}

public class Main {
    public static void main(String[] args) {
        Vehicle v1 = new Car();          // Polymorphism
        Vehicle v2 = new ElectricCar();  // Polymorphism

        v1.start();  // Output: Car starts with a key.
        v2.start();  // Output: Electric Car starts with a button.
    }
}
  • Abstraction: Vehicle defines what a vehicle can do (start).
  • Polymorphism: Each vehicle type defines how it starts differently.

4. Why Use Abstract Class Instead of Interface?

This is one of the most common interview questions. Both abstract classes and interfaces are used for abstraction, but they serve different purposes.

✅ Interface = Capability (“can do”)

Defines what a class can do, not how it does it. Used when unrelated classes share the same behavior.


interface Flyable {
    void fly();
}

class Bird implements Flyable {
    public void fly() {
        System.out.println("Bird flaps wings to fly.");
    }
}

class Airplane implements Flyable {
    public void fly() {
        System.out.println("Airplane uses engines to fly.");
    }
}

Here, Bird and Airplane are unrelated, but both can fly().

✅ Abstract Class = Base Type (“is a”)

Defines a common parent or base type for a group of related classes. Used when classes share common fields or code.


abstract class Vehicle {
    void stop() {
        System.out.println("Vehicle stopped.");
    }

    abstract void start();
}

class Car extends Vehicle {
    void start() {
        System.out.println("Car starts with a key.");
    }
}

class Bike extends Vehicle {
    void start() {
        System.out.println("Bike starts with a kick.");
    }
}

Summary: Use abstract class when classes are related. Use interface when classes are unrelated but share a capability.


5. Abstract Class vs Interface Comparison Table

FeatureAbstract ClassInterface
PurposeDefines a base for related classesDefines a capability or behavior
MethodsCan be abstract or concreteAbstract (can have default/static since Java 8)
VariablesCan have instance variablesOnly public static final constants
ConstructorYesNo
InheritanceSingle inheritanceMultiple inheritance supported
Use CaseVehicle → Car, BikeFlyable → Bird, Plane

6. Real-World Scenario — Payment Processing

Suppose you’re designing a payment system. You can use abstraction and polymorphism to handle multiple payment types easily.


abstract class PaymentProcessor {
    abstract void pay(double amount);
}

class PayPal extends PaymentProcessor {
    void pay(double amount) {
        System.out.println("Processing PayPal payment of $" + amount);
    }
}

class Stripe extends PaymentProcessor {
    void pay(double amount) {
        System.out.println("Processing Stripe payment of $" + amount);
    }
}

public class Main {
    public static void main(String[] args) {
        PaymentProcessor p1 = new PayPal();
        PaymentProcessor p2 = new Stripe();

        p1.pay(200.50);
        p2.pay(150.75);
    }
}

This design lets you add new payment providers easily without modifying your main code.


7. Key Points to Remember

🔹 Abstract Class

  • Used for related classes (hierarchical relationship).
  • Can have both abstract and concrete methods.
  • Can contain fields, constructors, and static methods.
  • Supports single inheritance.
  • Cannot be instantiated directly.

🔹 Interface

  • Defines behavior that can be shared by unrelated classes.
  • All methods are public and abstract by default (Java 7 and below).
  • Can have default and static methods (Java 8+).
  • No constructors or instance variables.
  • Supports multiple inheritance.

8. Coding Example — Employee Salary System

Let’s create an abstract class and demonstrate polymorphism.


abstract class Employee {
    String name;
    double baseSalary;

    Employee(String name, double baseSalary) {
        this.name = name;
        this.baseSalary = baseSalary;
    }

    abstract double calculateSalary();
}

class FullTimeEmployee extends Employee {
    double bonus;

    FullTimeEmployee(String name, double baseSalary, double bonus) {
        super(name, baseSalary);
        this.bonus = bonus;
    }

    double calculateSalary() {
        return baseSalary + bonus;
    }
}

class ContractEmployee extends Employee {
    int hoursWorked;
    double ratePerHour;

    ContractEmployee(String name, int hoursWorked, double ratePerHour) {
        super(name, 0);
        this.hoursWorked = hoursWorked;
        this.ratePerHour = ratePerHour;
    }

    double calculateSalary() {
        return hoursWorked * ratePerHour;
    }
}

public class Main {
    public static void main(String[] args) {
        Employee e1 = new FullTimeEmployee("Alice", 30000, 5000);
        Employee e2 = new ContractEmployee("Bob", 120, 200);

        System.out.println(e1.name + "'s Salary: " + e1.calculateSalary());
        System.out.println(e2.name + "'s Salary: " + e2.calculateSalary());
    }
}

Output:


Alice's Salary: 35000.0
Bob's Salary: 24000.0

This is a great example of Abstraction (common structure) and Polymorphism (different implementations).


9. Interview Questions

  • What is the difference between abstract class and interface?
  • Can we create an object of an abstract class?
  • Can an abstract class implement an interface?
  • Can we declare an abstract method as static or final?
  • Can an abstract class have a constructor?
  • How does polymorphism support abstraction?
  • Give a real-world example where you applied abstraction in your project.

10. Quick Verbal Interview Answer

“I use an abstract class when multiple related classes share common fields or methods — it provides partial abstraction and code reuse. I use an interface when I just want to define a capability or behavior that can be applied to unrelated classes — and I can implement multiple interfaces to achieve flexibility.”


Conclusion

That’s it! 🎉 You’ve learned what Abstract Classes are, how they differ from Interfaces, and how Polymorphism and Abstraction work together to build flexible, extensible Java applications.

By mastering these two OOP pillars, you can write clean, maintainable, and scalable code for any real-world problem.


Next Step: Try building a Notification System or Payment Gateway using abstraction and polymorphism to practice these concepts further.