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.
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
CopyTryDownload
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
Animal is abstract and cannot be instantiated directly.
The constructor initializes shared state.
makeSound() requires a child-specific implementation.
sleep() provides reusable behavior.
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.
The Document class is abstract because of printBody().
printHeader() is a final concrete method, so child classes cannot change it.
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.
The Employee class has a constructor that initializes the name.
The FullTimeEmployee constructor calls super(name) to execute the parent constructor.
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
The reference type is Payment (the abstract class).
The object created is CardPayment (the concrete subclass).
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
Device defines an abstract start() method.
MobileDevice extends Device but does not implement start(), remaining abstract itself while adding a new abstract method makeCall().
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
Report implements Exportable, but only provides the validate() method.
Because it doesn't implement the entire interface, Report must be abstract.
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.
Discussion
Ask questions, share suggestions, or discuss this lesson.