1. Introduction
An abstract class in Java is a class declared with the abstract keyword that cannot be instantiated on its own. It can mix abstract methods (declared but not implemented) with concrete methods (fully implemented), as well as instance fields and constructors.
Cricket analogy: Think of "Bowler" as an abstract class - you can't field a generic "Bowler" in your XI (uninstantiable), but it defines shared fields like run-up length and a concrete method like appealing, leaving the actual delivery action abstract for Bumrah or Shami to implement.
Abstract classes are used when related classes share common state and behavior, but some behavior must be specialized by each subclass.
Cricket analogy: Consider "Bowler" as an abstract class shared by Bumrah (pace) and Ashwin (spin) - both share fields like overs bowled but each specializes their run-up and delivery action differently.
2. Syntax
abstract class Shape {
protected String color;
// constructor is allowed in abstract classes
public Shape(String color) {
this.color = color;
}
// abstract method: no body, must be implemented by subclass
public abstract double area();
// concrete method: has a body, inherited as-is or overridden
public void describe() {
System.out.println("This shape is " + color);
}
}3. Explanation
Unlike an interface, an abstract class can declare instance fields with any access modifier, and can define constructors. A subclass constructor implicitly (or explicitly, via super(...)) calls the abstract class's constructor to initialize inherited state.
Cricket analogy: Just as a "Team" abstract class can declare a protected coachName field and a constructor setting up the season, a subclass like MumbaiIndians calls super() in its constructor to initialize that inherited coach data.
A concrete (non-abstract) subclass of an abstract class must implement every inherited abstract method, or the code will not compile. If a subclass does not implement all of them, that subclass must also be declared abstract.
Cricket analogy: If AllRounder extends the abstract Player class but leaves bowl() unimplemented, the code won't compile unless AllRounder itself is declared abstract, just as a half-picked squad can't take the field.
A class can extend only one abstract (or regular) class, since Java does not support multiple inheritance of implementation — this is the key distinction from interfaces, which a class may implement in any number.
Cricket analogy: A player class like Kohli can extend only one "Batsman" base class, just as a cricketer bats for only one franchise per season, though he can implement many interfaces like Fielder and Captain simultaneously.
Exam trap: You cannot create an object of an abstract class directly, e.g. new Shape("red") is a compile-time error, even though Shape has a constructor. The constructor exists only to be invoked via super() from a subclass.
An abstract class is not required to have any abstract methods at all — a class can be declared abstract purely to prevent direct instantiation, even if all its methods are concrete.
4. Example
abstract class Shape {
protected String color;
public Shape(String color) {
this.color = color;
}
public abstract double area();
public void describe() {
System.out.println("This is a " + color + " shape with area " + area());
}
}
class Circle extends Shape {
private double radius;
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle("red", 2.0);
s.describe();
}
}5. Output
This is a red shape with area 12.5663706143591726. Key Takeaways
- Abstract classes are declared with the abstract keyword and cannot be instantiated.
- They can hold both abstract methods (no body) and concrete methods (with body).
- They can have constructors and instance fields, unlike interfaces.
- A concrete subclass must implement all inherited abstract methods.
- A class can extend only one abstract class, unlike interfaces which allow multiple implementation.
Practice what you learned
1. Can an abstract class have a constructor?
2. What happens if a concrete subclass fails to implement an inherited abstract method?
3. Must every abstract class contain at least one abstract method?
4. How many classes can a subclass extend?
5. Which statement is true about abstract classes compared to interfaces?
Was this page helpful?
You May Also Like
Interfaces in Java
Learn how Java interfaces define a contract of behavior, support multiple inheritance of type, and how default/static methods work since Java 8.
Inheritance in Java
Understand Java inheritance using the extends keyword, the super keyword, single inheritance rules, and the Object root class.
Polymorphism in Java
Explore compile-time and runtime polymorphism in Java, including method overloading, overriding, and dynamic dispatch.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics