100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

Abstract Classes in Java

Understand Java abstract classes, how they mix abstract and concrete methods, and when to choose them over interfaces.

OOP AdvancedIntermediate9 min readJul 7, 2026
Analogies

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

java
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

java
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

text
This is a red shape with area 12.566370614359172

6. 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

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#AbstractClassesInJava#Abstract#Classes#Syntax#Explanation#OOP#StudyNotes#SkillVeris