Object Oriented Programming with Java

⌘K
  1. Home
  2. Docs
  3. Object Oriented Programmi...
  4. Inheritance and Interface
  5. Abstract Class in Java

Abstract Class in Java

Learn everything about Abstract Classes in Java — their definition, purpose, syntax, examples, and best practices. Understand how abstraction enhances OOP design and reduces redundancy in Java programming.


Abstract Class in Java: Definition, Examples, and Best Practices

In Java programming, abstraction is one of the four main pillars of Object-Oriented Programming (OOP), alongside inheritance, encapsulation, and polymorphism.
Among the tools that make abstraction possible, the abstract class plays a critical role.

In this guide, we’ll explore the concept of abstract classes in Java, their syntax, real-world applications, and differences from interfaces. You’ll also see how they improve code reusability, maintainability, and clarity — essential for developers aiming to master Java OOP principles.


What Is an Abstract Class in Java?

An abstract class in Java is a class declared using the abstract keyword that cannot be instantiated directly. It is designed to serve as a base or blueprint for other classes.

Instead of providing complete implementation, an abstract class typically defines abstract methods (methods without a body) that must be implemented by its subclasses.

Syntax:

abstract class ClassName {
    abstract void methodName();  // Abstract method
    void display() {             // Concrete method
        System.out.println("This is a concrete method.");
    }
}

Key Points:

  • An abstract class cannot be instantiated.
  • It can contain both abstract and concrete methods.
  • Subclasses must implement all abstract methods unless they are also declared abstract.
  • Abstract classes are used to define common behavior for multiple related subclasses.

Why Use Abstract Classes in Java?

Abstract classes promote code reuse and standardization by providing a template for subclasses.

They are most useful when you want:

  • To define a general structure but let subclasses provide specific behavior.
  • To enforce method implementation across multiple subclasses.
  • To share common methods and fields among related classes.

Example Scenario:

You have multiple types of payment systems — Credit Card, PayPal, and Bitcoin. Each has different logic, but all must have a method to process payment.
You can define an abstract class Payment with an abstract method processPayment() and let each subclass implement it differently.


Example: Abstract Class in Java

abstract class Payment {
    abstract void processPayment(double amount);

    void displayMessage() {
        System.out.println("Processing your payment...");
    }
}

class CreditCardPayment extends Payment {
    void processPayment(double amount) {
        System.out.println("Payment of $" + amount + " made via Credit Card.");
    }
}

class PayPalPayment extends Payment {
    void processPayment(double amount) {
        System.out.println("Payment of $" + amount + " made via PayPal.");
    }
}

public class Main {
    public static void main(String[] args) {
        Payment payment1 = new CreditCardPayment();
        payment1.displayMessage();
        payment1.processPayment(250.75);

        Payment payment2 = new PayPalPayment();
        payment2.displayMessage();
        payment2.processPayment(120.00);
    }
}

Output:

Processing your payment...
Payment of $250.75 made via Credit Card.
Processing your payment...
Payment of $120.0 made via PayPal.

Here, the abstract class Payment defines a common structure, while each subclass provides its own version of processPayment().


Features of Abstract Classes

FeatureDescription
Declared with abstract keywordThe class is marked as abstract to indicate incomplete implementation.
Cannot be instantiatedObjects cannot be created directly from abstract classes.
Can include both abstract and concrete methodsAbstract methods define structure, while concrete methods provide common behavior.
Can have constructors and fieldsUsed to initialize subclass objects or define shared attributes.
Supports inheritanceOther classes can extend it and provide implementations for abstract methods.

Abstract Methods in Java

An abstract method is a method without a body — it only has a declaration.

Syntax:

abstract returnType methodName(parameters);

Example:

abstract void draw();

Subclasses extending the abstract class must implement this method.


Abstract Class with Constructor

Though abstract classes cannot be instantiated, they can have constructors, which are called when a subclass object is created.

Example:

abstract class Animal {
    Animal() {
        System.out.println("Animal created.");
    }

    abstract void sound();
}

class Dog extends Animal {
    Dog() {
        System.out.println("Dog created.");
    }

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

public class Demo {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();
    }
}

Output:

Animal created.
Dog created.
Bark

The abstract class constructor runs before the subclass constructor.


Abstract Class vs Interface in Java

FeatureAbstract ClassInterface
Keyword Usedabstractinterface
MethodsCan have both abstract and concrete methodsAll methods are abstract (by default, until Java 8)
VariablesCan have instance variablesVariables are public, static, and final
ConstructorsCan have constructorsCannot have constructors
Inheritance TypeSupports single inheritanceSupports multiple inheritance (a class can implement multiple interfaces)
Use CaseWhen you want to share code and behaviorWhen you want to define a contract or capabilities

Example Use Case:

  • Use abstract class when subclasses share code (e.g., methods, constructors).
  • Use interface when classes are unrelated but must follow the same contract.

Real-World Example: Abstract Class

Let’s look at a real-world example involving vehicles.

abstract class Vehicle {
    abstract void start();
    abstract void stop();

    void fuelType() {
        System.out.println("This vehicle uses fuel.");
    }
}

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

    void stop() {
        System.out.println("Car stops using brake pedal.");
    }
}

class ElectricCar extends Vehicle {
    void start() {
        System.out.println("Electric Car starts silently.");
    }

    void stop() {
        System.out.println("Electric Car stops using regenerative braking.");
    }

    void fuelType() {
        System.out.println("This vehicle runs on electricity.");
    }
}

public class Main {
    public static void main(String[] args) {
        Vehicle tesla = new ElectricCar();
        tesla.start();
        tesla.fuelType();
        tesla.stop();
    }
}

Output:

Electric Car starts silently.
This vehicle runs on electricity.
Electric Car stops using regenerative braking.

This demonstrates polymorphism and abstraction using abstract classes.


Advantages of Abstract Classes

  • Encapsulation of common logic
  • Improved code reusability
  • Enhanced maintainability
  • Supports polymorphism
  • Encourages modular and extensible design

Limitations of Abstract Classes

  • Cannot support multiple inheritance directly.
  • Subclasses must implement all abstract methods.
  • May not be suitable when classes are unrelated but share behavior — in such cases, interfaces are better.

Best Practices for Using Abstract Classes

  • Use abstract classes when you want to share code among closely related classes.
  • Combine abstract and concrete methods strategically for flexibility.
  • Avoid making every method abstract — only abstract what must differ.
  • Prefer interfaces when dealing with unrelated class hierarchies.

Conclusion

Abstract classes are a foundational part of Object-Oriented Programming in Java.
They provide a blueprint for subclasses, enabling developers to enforce structure while maintaining flexibility in implementation.

By effectively using abstract classes, you can design Java applications that are modular, reusable, and scalable — essential traits for enterprise-grade software.


FAQs: Abstract Class in Java

1. Can we create an object of an abstract class in Java?
No, you cannot create an object of an abstract class directly. You can, however, create references of an abstract type.

2. Can an abstract class have a constructor?
Yes, abstract classes can have constructors that are called when a subclass is instantiated.

3. Can an abstract class have a main() method?
Yes, it can — but it cannot be used to create an object of the abstract class itself.

4. Can an abstract class implement an interface?
Yes, an abstract class can implement an interface and either provide or defer implementation to its subclasses.

5. How is an abstract class different from an interface?
An abstract class allows both implementation and abstraction, while an interface only defines a contract.

Tags , , , , , , , , , ,

How can we help?

Leave a Reply

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