Abstract Class in Java - Shared Base Classes and Abstract Methods

⏱️ 23 min read • Beginner Level • Lesson 57

Lesson 57 of 124 of Java Tutorial
You have completed this lesson

Completed on . You can revise this lesson or continue to the next topic.

An abstract class in Java is an incomplete base class designed to be extended. It can define required behavior for child classes while also sharing fields, constructors, and implemented methods.

Abstract classes are useful when related classes have common state or implementation but must provide different versions of one or more operations. They support abstraction, inheritance, code reuse, and runtime polymorphism.


What Is an Abstract Class in Java?

A class declared with the abstract keyword is called an abstract class. It cannot be instantiated directly. Instead, another class extends it and completes any required abstract behavior.

Simple Meaning: An abstract class is a partially completed blueprint that provides common features and leaves selected details for child classes.
An abstract class may contain:
  • Abstract methods without bodies.
  • Concrete methods with implementations.
  • Instance and static fields.
  • Constructors.
  • Static, final, private, protected, and public concrete methods where valid.
  • Nested classes, interfaces, enums, and records where supported.

Abstract Class Syntax

Syntax.java
abstract class ClassName {
    abstract void abstractMethod();

    void concreteMethod() {
        // implemented behavior
    }
}
Important: A method with the abstract keyword ends with a semicolon and does not have a method body.

Why Use an Abstract Class?

Shared State

Related child classes can reuse fields and constructor logic.

Shared Implementation

Concrete methods avoid repeating common logic.

Required Behavior

Abstract methods force concrete children to provide essential operations.

Polymorphism

Client code can work with the abstract parent type.

Basic Abstract Class Example

AnimalExample.java
Copy Try Download
abstract class Animal {
    private final String name;

    Animal(String name) {
        this.name = name;
    }

    String getName() {
        return name;
    }

    abstract void makeSound();

    void sleep() {
        System.out.println(name + " is sleeping");
    }
}

class Dog extends Animal {
    Dog(String name) {
        super(name);
    }

    @Override
    void makeSound() {
        System.out.println(getName() + " says Woof");
    }

    public static void main(String[] args) {
        Animal animal = new Dog("Bruno");
        animal.makeSound();
        animal.sleep();
    }
}

Output:

Bruno says Woof
Bruno is sleeping

Explanation

  1. Animal is abstract and cannot be instantiated directly.
  2. The constructor initializes shared state.
  3. makeSound() requires a child-specific implementation.
  4. sleep() provides reusable behavior.
  5. The abstract reference stores a concrete Dog object.

Abstract Methods

An abstract method defines a required operation but leaves its implementation to a child class. A concrete child must implement inherited abstract methods unless a suitable implementation is already inherited.

Rule Explanation
No method body The declaration ends with a semicolon.
Must belong to an abstract type A class containing an abstract method must be abstract.
Implemented by child A concrete child supplies the method body.
Cannot be private The child must be able to override it.
Cannot be final Final would prevent the required override.
Cannot be static Static methods are hidden, not overridden.

Concrete Methods in an Abstract Class

An abstract class may provide fully implemented methods. This is one of its main advantages when related child classes need common behavior.

Document.java
abstract class Document {
    abstract void printBody();

    final void printHeader() {
        System.out.println("ProwessApps Document");
    }

    static void showFormat() {
        System.out.println("Format: Digital");
    }
}

Explanation

  1. The Document class is abstract because of printBody().
  2. printHeader() is a final concrete method, so child classes cannot change it.
  3. showFormat() is a static concrete method that belongs to the class itself.
Note: Concrete methods may be static or final when appropriate. Only abstract methods have restrictions that require overriding.

Constructor in an Abstract Class

Although an abstract class cannot be instantiated directly, it can have constructors. A child constructor invokes the parent constructor to initialize the inherited part of the object.

EmployeeExample.java
abstract class Employee {
    private final String name;

    Employee(String name) {
        this.name = name;
        System.out.println("Employee constructor");
    }

    String getName() {
        return name;
    }

    abstract double calculateSalary();
}

class FullTimeEmployee extends Employee {
    private final double monthlySalary;

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

    @Override
    double calculateSalary() {
        return monthlySalary;
    }

    public static void main(String[] args) {
        Employee employee =
                new FullTimeEmployee("Ayan", 50000);

        System.out.println(employee.getName());
        System.out.println(employee.calculateSalary());
    }
}

Output:

Employee constructor
Ayan
50000.0

Explanation

  1. The Employee class has a constructor that initializes the name.
  2. The FullTimeEmployee constructor calls super(name) to execute the parent constructor.
  3. This allows the abstract parent class to securely manage and initialize its own fields.

Fields and Shared State

Abstract classes may contain instance fields and static fields. Use private fields with controlled methods when shared state must remain protected.

  • Instance fields belong to each concrete child object.
  • Static fields belong to the abstract class itself.
  • Final fields can be initialized in the abstract class constructor.
  • Private fields support encapsulation across the class hierarchy.

Abstract Class References

While an abstract class cannot produce an object directly, its reference type can point to a concrete child object. This supports runtime polymorphism.

ReferenceExample.java
abstract class Payment {
    abstract void pay(double amount);
}

class CardPayment extends Payment {
    @Override
    void pay(double amount) {
        System.out.println("Card payment: ₹" + amount);
    }
}

class ReferenceExample {
    public static void main(String[] args) {
        Payment payment = new CardPayment();
        payment.pay(1200);
    }
}

Output:

Card payment: ₹1200.0

Explanation

  1. The reference type is Payment (the abstract class).
  2. The object created is CardPayment (the concrete subclass).
  3. Calling payment.pay(1200) triggers the subclass's overridden method due to runtime polymorphism.

Multilevel Abstract Classes

An abstract child class does not need to implement every inherited abstract method. It can refine the hierarchy and leave remaining work to a later concrete subclass.

MultilevelExample.java
abstract class Device {
    abstract void start();
}

abstract class MobileDevice extends Device {
    abstract void makeCall();
}

class Smartphone extends MobileDevice {
    @Override
    void start() {
        System.out.println("Smartphone starts");
    }

    @Override
    void makeCall() {
        System.out.println("Calling a contact");
    }
}

Explanation

  1. Device defines an abstract start() method.
  2. MobileDevice extends Device but does not implement start(), remaining abstract itself while adding a new abstract method makeCall().
  3. Smartphone is a concrete class, so it must implement both inherited abstract methods.

Abstract Class Implementing an Interface

An abstract class may implement an interface without implementing every abstract interface method. A concrete child must complete the remaining contract.

ReportExample.java
interface Exportable {
    void export();
    void validate();
}

abstract class Report implements Exportable {
    @Override
    public void validate() {
        System.out.println("Report validated");
    }
}

class PdfReport extends Report {
    @Override
    public void export() {
        System.out.println("PDF exported");
    }

    public static void main(String[] args) {
        Exportable report = new PdfReport();
        report.validate();
        report.export();
    }
}

Output:

Report validated
PDF exported

Explanation

  1. Report implements Exportable, but only provides the validate() method.
  2. Because it doesn't implement the entire interface, Report must be abstract.
  3. PdfReport extends Report and completes the contract by implementing export().

Important Abstract Class Rules

  • An abstract class is declared with abstract.
  • It cannot be instantiated directly.
  • It may contain zero or more abstract methods.
  • A class containing an abstract method must be abstract.
  • A concrete child must implement inherited abstract methods.
  • An abstract class can extend another abstract or concrete class.
  • It can implement one or more interfaces.
  • It can have constructors, fields, concrete methods, and static methods.
  • An abstract class cannot be final.
  • An abstract method cannot be private, static, or final.

Abstract Class vs Concrete Class

Feature Abstract Class Concrete Class
Instantiation Cannot be instantiated directly. Can be instantiated when accessible.
Abstract methods May contain abstract methods. Cannot contain unimplemented abstract methods.
Purpose Defines a shared incomplete base. Provides complete object behavior.
Reference type Can reference concrete child objects. Can reference its own or child objects.

Abstract Class vs Interface

Feature Abstract Class Interface
Main purpose Shared base for closely related classes. Capability or contract for implementing classes.
Instance state Allowed. No normal instance fields.
Constructors Allowed. Not allowed.
Methods Abstract and concrete methods. Abstract, default, static, and private methods where supported.
Inheritance count A class extends only one class. A class may implement multiple interfaces.
Access modifiers Class members may use normal valid access levels. Ordinary abstract methods are public; private helpers are supported in modern Java.

When Should You Use an Abstract Class?

  • Child classes are closely related and represent an “is-a” hierarchy.
  • Several child classes need the same fields or constructor logic.
  • You want to share substantial implementation.
  • You need non-public helper methods or protected extension points.
  • You want to require selected operations while implementing others centrally.
Choose carefully: If unrelated classes only need to share a capability, an interface may provide a more flexible design.

Tricky Cases

1. Can an abstract class have no abstract method?

Yes. It may be abstract merely to prevent direct instantiation or establish a shared base type.

2. Can an abstract class be final?

No. Abstract requires extension to complete or use the type, while final prevents extension.

3. Can an abstract class have a private constructor?

It may declare a private constructor, but subclasses cannot invoke that constructor directly. Another accessible constructor is needed for ordinary external subclassing.

4. Can an abstract class have a main() method?

Yes. Static methods, including main(), belong to the class and can run without instantiating it.

5. Can an abstract class have final methods?

Yes. A concrete final method supplies shared behavior that subclasses cannot override.

6. Can a constructor be abstract?

No. Constructors are not inherited or overridden, so an abstract constructor is not permitted.

7. Can an abstract class extend a concrete class?

Yes. It may inherit concrete behavior and add abstract requirements for later subclasses.

Common Mistakes

  • Trying to instantiate an abstract class.
  • Forgetting to declare a class abstract when it contains an abstract method.
  • Leaving required methods unimplemented in a concrete child.
  • Adding a method body to an abstract method declaration.
  • Declaring an abstract method private, static, or final.
  • Declaring the abstract class final.
  • Assuming an abstract class cannot have constructors or fields.
  • Using inheritance between classes that do not represent a meaningful relationship.
Summary:
  • An abstract class is an incomplete base class that cannot be instantiated directly.
  • It may contain abstract methods and concrete methods.
  • It can also contain constructors, fields, static methods, and final concrete methods.
  • Concrete children must implement inherited abstract methods.
  • Abstract references support runtime polymorphism.
  • An abstract class can partially implement an interface.
  • An abstract class cannot be final.
  • Use an abstract class when related types need shared state or implementation.

Interview Questions ⭐

An abstract class is a class declared with the abstract keyword. It cannot be instantiated directly and may contain abstract methods, concrete methods, fields, constructors, and static methods.

An abstract class is used when related child classes need a common base with shared state or implementation, while selected operations must be implemented differently by concrete subclasses.

No. An abstract class cannot be instantiated directly, but an abstract-class reference can refer to an object of a concrete subclass.

An abstract method declares a method signature without a body. A concrete subclass must implement it unless a suitable implementation is already inherited.

Yes. An abstract class can contain fully implemented concrete methods in addition to abstract methods.

Yes. A class may be declared abstract to prevent direct instantiation or to serve as a common base type even when it contains no abstract methods.

Yes. Its constructor runs when a concrete subclass object is created and initializes the inherited part of that object.

Yes. An abstract class may contain instance fields, static fields, and final fields.

Yes. An abstract class may contain static methods, but a static method cannot be abstract because static methods are hidden rather than overridden.

Yes. A concrete final method can provide shared behavior that subclasses are not allowed to override.

No. An abstract class is designed to be extended, while final prevents inheritance.

No. A private method is not available to subclasses as an overridable member, so it cannot be abstract.

No. Abstract methods require overriding in a subclass, whereas static methods belong to the class and are hidden rather than overridden.

No. Abstract requires a subclass implementation, while final prevents overriding.

No. Constructors are not inherited or overridden, so they cannot be declared abstract.

The compiler reports an error unless the subclass is also declared abstract or a suitable implementation is already inherited.

Yes. An abstract subclass may leave inherited abstract methods unimplemented for a later concrete subclass.

Yes. An abstract class can extend either an abstract class or a concrete class, subject to Java's single class inheritance rule.

Yes. It can implement all, some, or none of the interface's abstract methods. A concrete subclass must complete any remaining methods.

Yes. A main method is static and can run without creating an instance of the abstract class.

Yes, but subclasses cannot directly invoke that private constructor. Another accessible constructor is generally needed for ordinary subclassing outside the class.

Yes. An abstract-class reference can point to a concrete subclass object, enabling runtime polymorphism.

An abstract class cannot be instantiated directly and may contain unimplemented abstract methods. A concrete class provides complete required behavior and can be instantiated when accessible.

An abstract class can provide instance state, constructors, access-controlled members, and substantial shared implementation. An interface primarily defines a capability or contract and can be implemented alongside other interfaces.

Prefer an abstract class when closely related subclasses need shared fields, constructor logic, protected helpers, or substantial common implementation.

Client code can use an abstract parent reference while the concrete subclass implementation of an overridden method is selected at runtime.

Yes. An abstract class can contain concrete private helper methods, but a private method cannot be abstract.

Yes. It may contain nested classes, interfaces, enums, and records where supported by the Java version and declaration context.

Next step: Learn Java Interface

Continue to Java Interface →

Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Abstract-class | Language: Java

Question 1 of 25
Q1. Can an abstract class have no abstract methods?
Q2. Can an abstract method be private?
Q3. Can an abstract class extend a concrete class?
Q4. Can an abstract class be final?
Q5. Can an abstract subclass leave an inherited abstract method unimplemented?
Q6. Which is the best abstract-class design guideline?
Q7. Which feature can an abstract class provide that an interface does not provide in the same way?
Q8. What must a concrete subclass do with inherited abstract methods?
Q9. When is an abstract class generally preferred?
Q10. Can an abstract class contain static methods?
Q11. Can an abstract class implement an interface without implementing every method?
Q12. Which statement about abstract-class references is correct?
Q13. Can an abstract class have a constructor?
Q14. Can an abstract class have a main() method?
Q15. What is the purpose of an abstract method?
Q16. Can an abstract method be static?
Q17. Can a constructor be abstract?
Q18. Can an abstract class contain final concrete methods?
Q19. Can an abstract method be final?
Q20. What is the output of the constructor-order example?
abstract class Parent {
    Parent() {
        System.out.println("Parent");
    }
}

class Child extends Parent {
    Child() {
        System.out.println("Child");
    }

    public static void main(String[] args) {
        new Child();
    }
}
Q21. Which keyword declares an abstract class in Java?
Q22. Can an abstract class be instantiated directly?
Q23. Which statement about an abstract class is correct?
Q24. What is the output of the abstract-reference example?
abstract class Animal {
    abstract void sound();
}

class Dog extends Animal {
    void sound() {
        System.out.println("Woof");
    }

    public static void main(String[] args) {
        Animal animal = new Dog();
        animal.sound();
    }
}
Q25. What happens when this code is compiled?
abstract class Animal {
    abstract void sound();
}

class Dog extends Animal {
}

Great job! Continue learning Java OOP step by step.

Discussion

Ask questions, share suggestions, or discuss this lesson.

Please login or create an account to join the discussion and save your learning activity.

Loading comments...

Have you completed this lesson?

Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.

Not completed yet.