Abstract Class in Java: A Complete Guide for Beginners
Jun 11, 2026 5 Min Read 14 Views
(Last Updated)
Table of contents
- Quick TL;DR
- Introduction
- What Is an Abstract Class in Java?
- Syntax of an Abstract Class in Java
- How Does an Abstract Class Work?
- Step 1: Declare the abstract class
- Step 2: Create a subclass
- Step 3: Instantiate the subclass
- Can an Abstract Class Have a Constructor?
- What Are the Rules for Abstract Classes in Java?
- Real-World Example of an Abstract Class
- When Should You Use an Abstract Class in Java?
- Common Mistakes When Using Abstract Classes
- Conclusion
- FAQs
- What is an abstract class in Java?
- Can we create an object of an abstract class in Java?
- What is the difference between an abstract class and an interface in Java?
- Can an abstract class have a constructor in Java?
- Can an abstract class implement an interface in Java?
- Can an abstract class have static methods in Java?
- What happens if a subclass does not implement all abstract methods?
- What is the use of an abstract class in real-world Java projects?
Quick TL;DR
An abstract class in Java is a class that cannot be instantiated on its own and is designed to be extended by other classes. It acts as a blueprint, allowing you to define common behaviour and force subclasses to implement specific methods. Abstract classes can contain both abstract methods (without a body) and regular methods (with a body). They are widely used in Java object-oriented programming to achieve abstraction, reduce code repetition, and enforce a consistent structure across related classes.
Introduction
Many beginners write Java classes without realising they are missing a cleaner way to share common behaviour across related objects. Abstract class in Java solves exactly that by letting you define a base structure that subclasses must follow. Understanding how and when to use abstract classes will make your Java code more organised, reusable, and interview-ready.
What Is an Abstract Class in Java?
An abstract class in Java is a class declared with the abstract keyword. It cannot be instantiated directly, meaning you cannot create an object of an abstract class. Instead, it is meant to be inherited by subclasses.
An abstract class can contain:
- Abstract methods (methods with no body)
- Concrete methods (regular methods with a body)
- Constructors
- Instance variables
- Static methods
This flexibility makes abstract classes more powerful than interfaces in certain situations, especially when you want to share common code across related classes.
Read More: Java Interview Questions for Freshers with Clear & Concise Answers
Syntax of an Abstract Class in Java
The syntax is straightforward. You use the abstract keyword before the class declaration.
abstract class Animal {
String name;
// Abstract method – no body
abstract void makeSound();
// Concrete method – has a body
void breathe() {
System.out.println(name + ” is breathing.”);
}
}
Any class that extends Animal must provide an implementation for makeSound(). The breathe() method is inherited as-is by all subclasses.
How Does an Abstract Class Work?
Now let’s understand how abstract classes function step by step.
Step 1: Declare the abstract class
Use the abstract keyword when defining the class. Any method you want to force subclasses to implement should also be marked abstract.
Step 2: Create a subclass
A subclass extends the abstract class using the extends keyword. The subclass must override all abstract methods, or it must also be declared abstract.
Step 3: Instantiate the subclass
You cannot create an object of the abstract class itself. You create objects of the subclass instead.
Here is a complete example:
abstract class Animal {
String name;
abstract void makeSound();
void breathe() {
System.out.println(name + ” is breathing.”);
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println(“Dog barks.”);
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.name = “Bruno”;
d.makeSound(); // Output: Dog barks.
d.breathe(); // Output: Bruno is breathing.
}
}
The Dog class provides its own implementation of makeSound(), while inheriting breathe() directly from Animal.
Can an Abstract Class Have a Constructor?
Yes, and this surprises many beginners. An abstract class can have a constructor even though you cannot instantiate it directly.
The constructor in an abstract class is called when a subclass object is created. It is useful for initialising common fields shared by all subclasses.
abstract class Vehicle {
String brand;
Vehicle(String brand) {
this.brand = brand;
}
abstract void fuelType();
}
class ElectricCar extends Vehicle {
ElectricCar(String brand) {
super(brand);
}
@Override
void fuelType() {
System.out.println(brand + ” runs on electricity.”);
}
}
Here, the constructor in Vehicle initialises the brand field for every subclass automatically.
Since Java 8, interfaces have become far more powerful by supporting default and static methods with full implementations, allowing developers to add new functionality without breaking existing code. However, abstract classes still play a unique role in object-oriented design because they can define constructors, maintain instance variables, and manage shared state across related subclasses. While interfaces are ideal for defining capabilities that multiple unrelated classes can implement, abstract classes remain the preferred choice when a group of classes shares common data and behavior. This distinction is why modern Java applications often use both interfaces and abstract classes together to achieve flexible yet maintainable software architectures.
What Are the Rules for Abstract Classes in Java?
Before writing abstract classes, keep these rules in mind:
- A class must be declared abstract if it contains even one abstract method.
- An abstract class cannot be instantiated using the new keyword.
- A subclass must implement all abstract methods, or it must also be declared abstract.
- An abstract class can extend another abstract class without implementing its abstract methods.
- An abstract class can implement an interface without providing method implementations.
- Abstract methods cannot be private, static, or final.
Object-Oriented Programming (OOP) concepts such as abstraction, inheritance, encapsulation, and polymorphism form the foundation of most Java applications and are frequently evaluated during technical interviews. Among these concepts, abstract classes play a key role in designing reusable and maintainable code by allowing developers to define shared behavior while leaving specific implementations to subclasses. Because many enterprise frameworks and large-scale Java applications rely heavily on OOP principles, a solid understanding of abstract classes and when to use them is considered an essential skill for aspiring Java developers preparing for internships, entry-level roles, and software engineering interviews.
Real-World Example of an Abstract Class
Let’s look at how abstract classes are used in a practical scenario.
Consider a payment processing system. Different payment methods like credit card, UPI, and net banking share common steps: validate the transaction, process the payment, and generate a receipt. But the actual processing logic differs for each method.
abstract class Payment {
String transactionId;
Payment(String transactionId) {
this.transactionId = transactionId;
}
abstract void processPayment(double amount);
void generateReceipt() {
System.out.println(“Receipt generated for transaction: ” + transactionId);
}
}
class UPIPayment extends Payment {
UPIPayment(String transactionId) {
super(transactionId);
}
@Override
void processPayment(double amount) {
System.out.println(“Processing UPI payment of Rs. ” + amount);
}
}
class CreditCardPayment extends Payment {
CreditCardPayment(String transactionId) {
super(transactionId);
}
@Override
void processPayment(double amount) {
System.out.println(“Processing credit card payment of Rs. ” + amount);
}
}
Here, the abstract class Payment defines the shared structure. Each subclass handles its own payment logic. This pattern is commonly used by fintech companies and payment gateways to maintain a consistent transaction flow across multiple payment modes.
When Should You Use an Abstract Class in Java?
Use an abstract class when:
- Multiple related classes share common code or fields.
- You want to force subclasses to implement certain methods.
- You need constructors or instance variables (not possible with interfaces).
- You are modelling a clear parent-child relationship, such as Animal and Dog.
Avoid using an abstract class when:
- The classes are unrelated but need to follow the same contract. Use an interface instead.
- You need multiple inheritance. Implement multiple interfaces instead.
Common Mistakes When Using Abstract Classes
1. Trying to instantiate an abstract class: Many beginners attempt to use new AbstractClassName() directly. This will throw a compile-time error. Always instantiate the subclass, not the abstract class.
2. Forgetting to implement abstract methods in the subclass: If a subclass does not override all abstract methods from the parent, the subclass itself must be declared abstract. Missing this step causes a compilation error.
3. Marking abstract methods as private: Abstract methods must be overridden by subclasses, so they cannot be private. Use protected or the default (package-private) access modifier instead.
4. Confusing abstract classes with interfaces: Abstract classes are best for related classes sharing common behaviour. Interfaces are best for defining a contract across unrelated classes. Using an abstract class when an interface is more appropriate adds unnecessary coupling to your code.
5. Not calling the superclass constructor: When an abstract class has a parameterised constructor, subclasses must call it using super(). Forgetting this causes a compilation error.
Want to become a full stack developer and build modern web applications? Check out HCL GUVI’s IITM Pravartak Certified MERN Full Stack Developer Program with AI Integration, designed for beginners to master MongoDB, Express.js, React, Node.js, AI-powered development, and real-world projects with expert guidance and career support.
Conclusion
As Java continues to power enterprise applications, Android development, and backend systems worldwide, writing clean object-oriented code remains a core skill for every developer.
Mastering abstract classes, understanding when to use them over interfaces, and practising with real-world examples like payment systems or vehicle hierarchies can significantly strengthen your Java foundation. Start with a simple abstract class, build a subclass, run it, and experiment with adding constructors and concrete methods.
FAQs
1. What is an abstract class in Java?
An abstract class in Java is a class declared with the abstract keyword that cannot be instantiated directly. It is used as a base class to provide common functionality and define abstract methods that subclasses must implement.
2. Can we create an object of an abstract class in Java?
No, you cannot create an object of an abstract class using the new keyword. You must create an object of a concrete subclass that extends the abstract class.
3. What is the difference between an abstract class and an interface in Java?
An abstract class can have both abstract and concrete methods, instance variables, and constructors. An interface can only have abstract methods by default (and default/static methods since Java 8) and does not allow instance variables or constructors.
4. Can an abstract class have a constructor in Java?
Yes, an abstract class can have a constructor. It is called automatically when a subclass object is created, typically to initialise shared fields using super().
5. Can an abstract class implement an interface in Java?
Yes, an abstract class can implement an interface without providing implementations for all the interface methods. The responsibility of implementing those methods is passed down to the concrete subclasses.
6. Can an abstract class have static methods in Java?
Yes, abstract classes can have static methods. Static methods belong to the class itself, not to any instance, so they work normally in abstract classes and can be called directly using the class name.
7. What happens if a subclass does not implement all abstract methods?
If a subclass does not override all abstract methods from its parent abstract class, the subclass itself must be declared abstract. Otherwise, the code will not compile.
8. What is the use of an abstract class in real-world Java projects?
Abstract classes are widely used in Java frameworks and enterprise applications to define common behaviour. For example, payment processing systems use abstract classes to define a standard transaction flow while allowing individual payment methods to handle their own logic.



Did you enjoy this article?