An abstract class in Java is a class declared with the abstract keyword that cannot be instantiated directly. It acts as a blueprint for subclasses, defining shared structure through concrete methods and enforcing a contract through abstract methods that every subclass must implement. It can hold fields, constructors and both abstract and non-abstract methods.
Beyond that definition, an abstract class in Java is a class declared with the abstract keyword that cannot be instantiated directly. It acts as a blueprint for other classes, defining a common structure while leaving specific behaviour for subclasses to implement. Think of it as a template: you sketch the rules, and every class that extends yours must fill in the details.
- Key Takeaway 1: An abstract class in Java can contain both abstract methods (no body) and concrete methods (with a body).
- Key Takeaway 2: You cannot create an object of an abstract class using
new; you must extend it and instantiate the subclass. - Key Takeaway 3: Abstract classes can have constructors, fields, static methods and access modifiers, just like regular classes.
- Key Takeaway 4: The abstract keyword in Java enforces a contract: every concrete subclass must override all abstract methods or itself be declared abstract.
- Key Takeaway 5: Abstract classes in Java are ideal when multiple related classes share common code but also need their own specific implementations.
What an Abstract Class in Java Is
The abstract class definition in Java is straightforward: it is a class that is incomplete by design. The abstract keyword signals to the Java compiler that this class is meant to be extended, not used directly. According to the Oracle Java documentation, an abstract class may or may not include abstract methods, but any class with at least one abstract method must be declared abstract.
Java consistently ranks among the top five most-used programming languages globally. The TIOBE Index for 2024 places Java at position 4, and the Stack Overflow Developer Survey 2024 shows that roughly 30% of professional developers use Java regularly. According to the JetBrains State of Developer Ecosystem 2023 report, Java remains the primary language for 48% of backend developers worldwide, underlining why mastering object-oriented programming concepts like abstract classes in Java is critical for career growth. Understanding abstract classes is one of those fundamentals that separates junior developers from senior ones in technical interviews at companies like Infosys, TCS and Wipro.
Abstract Class Syntax in Java
Here is the basic abstract class syntax in Java:
| Syntax Element | Purpose | Required? |
|---|---|---|
abstract class ClassName |
Declares the class as abstract | Yes |
abstract returnType methodName(); |
Declares a method with no body | Optional |
returnType methodName() { ... } |
Concrete method with full implementation | Optional |
| Constructor inside abstract class | Called via super() from subclass |
Optional |
| Fields and static members | Shared state across subclasses | Optional |
A minimal example looks like this. The class Shape is declared abstract. It has one abstract method area() and one concrete method display(). Any class that extends Shape must provide its own area() logic, or it too must be marked abstract.
What You Can and Cannot Do With an Abstract Class in Java
You cannot write Shape s = new Shape();. The compiler throws an error immediately: “Shape is abstract; cannot be instantiated.” This is intentional. The class is designed to be a parent, not an object in its own right.
You can declare a reference of the abstract type, though. Writing Shape s = new Circle(); works perfectly, as long as Circle extends Shape and implements area(). This is the foundation of polymorphism in Java, and it is one reason abstract classes appear so often in large-scale enterprise codebases.
If you are just getting started with Java and want to understand how compilation and execution work end-to-end, the guide on how to write, compile and run a C program gives you a useful comparison of the compile-run cycle across languages. Before diving deeper into Java OOP, it also helps to know which software development tools to learn first so your environment is set up correctly from day one.
Abstract Methods and the abstract Keyword in Java
An abstract method is a method declared without a body. It ends with a semicolon rather than curly braces. The syntax is simple: abstract void draw();. When a subclass inherits this method, it must override it and provide an actual implementation, otherwise the code will not compile.
The abstract keyword in Java can only be applied to classes and methods, not to variables, constructors or static methods. Trying to mark a static method abstract is a compile-time error because static methods belong to the class itself, not to any overriding subclass.
Concrete Methods Inside an Abstract Class in Java
One of the most practical aspects of an abstract class in Java is that it can contain fully implemented concrete methods. This is the template method pattern in action. You define the skeleton of an algorithm in the abstract class using concrete methods, and you leave the steps that vary to the subclasses via abstract methods.
Say you are building a payment processing system for an Indian e-commerce platform. The abstract class PaymentProcessor has a concrete method processPayment() that handles logging, validation and receipt generation. The abstract method deductAmount() is left to subclasses like UPIPayment or NetBankingPayment to implement. This keeps shared logic in one place and forces each payment type to define its own deduction logic — a pattern used widely in Indian fintech applications built on Java.
Common Mistakes Developers Make With Abstract Classes in Java
The most frequent mistake is forgetting to override all abstract methods in a subclass. If even one abstract method is missing, the subclass must also be declared abstract, or the compiler will refuse to build the project. In a large team, this can cause confusing build failures if a new developer adds an abstract method to a parent class without updating all child classes.
A second common error is trying to use the abstract and final keywords together on a class or method. They directly contradict each other: abstract means “must be overridden” while final means “cannot be overridden.” Java will throw a compile error if you attempt this combination. Knowing errors like these before interviews saves you real embarrassment. The 3.0 University guide on Java exceptions and common runtime errors covers more of these traps in depth.
Abstract Class in Java Example Program
The following is a complete, runnable abstract class program in Java that demonstrates the template method pattern. Read through the structure before running it.
// Abstract class in Java
abstract class Vehicle {
String brand;
// Constructor in abstract class
Vehicle(String brand) {
this.brand = brand;
}
// Abstract method - no body
abstract void fuelType();
// Concrete method - shared logic
void displayBrand() {
System.out.println("Brand: " + brand);
}
}
// Concrete subclass 1
class ElectricCar extends Vehicle {
ElectricCar(String brand) {
super(brand);
}
@Override
void fuelType() {
System.out.println(brand + " runs on electricity.");
}
}
// Concrete subclass 2
class PetrolBike extends Vehicle {
PetrolBike(String brand) {
super(brand);
}
@Override
void fuelType() {
System.out.println(brand + " runs on petrol.");
}
}
// Main class
public class Main {
public static void main(String[] args) {
Vehicle v1 = new ElectricCar("Tata Nexon EV");
Vehicle v2 = new PetrolBike("Royal Enfield");
v1.displayBrand();
v1.fuelType();
v2.displayBrand();
v2.fuelType();
}
}
Output
Brand: Tata Nexon EV
Tata Nexon EV runs on electricity.
Brand: Royal Enfield
Royal Enfield runs on petrol.
Breaking Down the Abstract Class Java Example
Notice that Vehicle has a constructor even though it is abstract. This is perfectly valid. The constructor runs when a subclass calls super(brand), which initialises the brand field for all vehicles without duplicating that code in every subclass.
The reference type is Vehicle, but the actual objects are ElectricCar and PetrolBike. Java’s runtime polymorphism kicks in and calls the correct fuelType() version for each object. This is exactly how large frameworks like Spring and Hibernate use abstract classes internally to give developers extensible, predictable APIs.
Students studying BSc Computer Science subjects and scope will encounter this pattern repeatedly across object-oriented design, software engineering and advanced Java modules. Getting comfortable with it early pays off across your entire degree and career.
When to Use an Abstract Class in Java
Choosing between an abstract class in Java and an interface is one of the most common design decisions in Java. The short rule: use an abstract class when related classes share state (fields) or common method implementations. Use an interface when unrelated classes need to agree on a contract without sharing code. The table below captures the decision logic quickly.
| Situation | Use Abstract Class in Java? | Reason |
|---|---|---|
| Multiple related classes share fields and methods | Yes | Avoids duplicating shared state |
| You want a template method pattern | Yes | Concrete methods define the skeleton |
| Unrelated classes need a common contract | No | Interface is more appropriate |
| You need multiple inheritance of type | No | Java classes can only extend one class |
| You want to enforce method implementation in child classes | Yes | Abstract methods guarantee override |
Real-World Scenarios Where Abstract Classes in Java Shine
Game development is a classic use case. An abstract class GameCharacter can define shared behaviour like move() and takeDamage(), while leaving attack() abstract so that Warrior, Archer and Mage each implement their own combat style.
In enterprise Java, frameworks like Hibernate use abstract classes extensively for base entity mappings. Your project’s BaseEntity class might hold id, createdAt and updatedAt fields with concrete getters, while leaving domain-specific validation abstract. This keeps audit logic centralised and forces every entity to define its own validation rules.
For those exploring career paths in software, understanding these design patterns is a core expectation at mid-level roles. If you are weighing your options, the comparison between a full stack developer vs software developer role is a useful read for understanding where Java OOP skills matter most. And if you are an MCA graduate mapping your options, the breakdown of jobs and salary after MCA shows how Java expertise directly affects starting packages in India, where Java developer salaries range from Rs 4 LPA at entry level to Rs 18 LPA or more at senior level according to AmbitionBox 2024 data.
A Practical Tip From the Field
One thing that trips up developers on the job: adding a new abstract method to an existing abstract class in a shared library breaks every concrete subclass that does not implement it. If your abstract class is part of a public API, think twice before adding abstract methods. Instead, add a concrete method with a default behaviour. This is how the Java standard library itself evolves without breaking backward compatibility.
Strong fundamentals in Java OOP are also the bedrock for cybersecurity engineering. Secure coding in Java, Android malware analysis and writing exploit detection tools all require you to understand class hierarchies deeply. If you want to build on this foundation for a career in ethical hacking or AI security, the CEH v13 AI online course is a direct next step, and you can browse the full 3.0 University online certification courses catalogue to find the right path.
Frequently Asked Questions
What is an abstract class in Java?
An abstract class in Java is a class declared with the abstract keyword that cannot be instantiated on its own. It serves as a base class, providing a common structure through concrete methods and enforcing a contract through abstract methods that every subclass must implement. It can contain fields, constructors and both abstract and non-abstract methods.
What is an abstract method in Java?
An abstract method is a method declared inside an abstract class without any body, ending with a semicolon instead of curly braces. For example: abstract void draw();. Every non-abstract subclass that inherits this method is required to override it and provide a complete implementation, or the code will not compile.
Can an abstract class have a constructor in Java?
Yes, an abstract class in Java can have a constructor. It is called automatically when a subclass object is created, typically via super() from the subclass constructor. The constructor in an abstract class is used to initialise shared fields that all subclasses will inherit, avoiding repeated initialisation code across every child class.
Can we create an object of an abstract class in Java?
No, you cannot directly instantiate an abstract class using the new keyword. Attempting Shape s = new Shape(); produces a compile-time error: “Shape is abstract; cannot be instantiated.” You must create an object of a concrete subclass that extends the abstract class and implements all its abstract methods.
What is the syntax of an abstract class in Java?
The syntax starts with the abstract keyword before the class keyword: abstract class ClassName { abstract void methodName(); }. Inside, you can declare abstract methods (no body), concrete methods (with body), constructors and fields. A subclass uses the extends keyword to inherit the abstract class and must override all abstract methods.
What is the difference between abstract class and interface in Java?
An abstract class in Java can have fields, constructors and concrete methods with shared logic, and a class can extend only one abstract class. An interface cannot hold instance fields or constructors, and a class can implement multiple interfaces. Use an abstract class when related classes share state or behaviour; use an interface when unrelated classes need a common contract.
Can an abstract class implement an interface in Java?
Yes, an abstract class in Java can implement an interface without providing implementations for all the interface methods. The unimplemented interface methods are simply treated as additional abstract methods that concrete subclasses must override. This is a common pattern in Java frameworks where an abstract class partially implements an interface and leaves domain-specific methods to subclasses.
What you have seen in this article is the core of how Java enforces design contracts through abstract classes. The template method pattern, shared constructors, concrete helpers and enforced overrides all come together to make your code predictable and scalable.
- This week: Write your own abstract class in Java with at least two abstract methods and two concrete subclasses. Use a real-world scenario from your field, whether it is a university grade system, a payment app or a game character.
- Next: Study the difference between abstract classes and interfaces in Java. They solve related but distinct design problems, and every Java interview at product companies will test your ability to choose between them.
- Then: If you want to apply your Java and OOP skills in cybersecurity or AI, explore 3.0 University’s CEH v13 AI online course or browse the full 3.0 University online certification courses. Secure coding, ethical hacking, AI and blockchain development all build directly on the programming foundations you are building right now.
Last updated: June 2025. Reviewed by the 3University editorial team.