01 / 21

Abstract Classes
in Java

Learn how to define shared behavior using abstract classes, enforce method contracts with abstract methods, and build flexible class hierarchies.

Java

What Is an Abstract Class?

An abstract class is a class that cannot be instantiated directly. It serves as a blueprint for other classes. You declare it with the abstract keyword.

public abstract class Shape { // Abstract method: no body public abstract double area(); // Concrete method: has a body public void describe() { System.out.println("I am a shape."); } }

Why Use Abstract Classes?

Shared Code
Write common logic once in the abstract class. All subclasses inherit it without duplication.
Enforced Contracts
Abstract methods force every subclass to provide its own implementation: no forgetting allowed.
Flexible Design
You can reference subclasses through the abstract type, enabling polymorphism and cleaner code.

Anatomy of an Abstract Class

<<abstract>> Shape + area() : double {abstract} + describe() : void {concrete} Circle - radius : double + area() : double overrides inherits describe() Rectangle - width, height : double + area() : double overrides inherits describe()

The Rules of Abstract Classes

1
Cannot be instantiated. You cannot write new Shape(): the compiler will reject it.
2
Can have constructors. Subclasses call them via super() to initialize shared fields.
3
Can mix abstract and concrete methods. Abstract methods have no body: concrete methods have full implementations.
4
Subclasses must override all abstract methods unless the subclass is also declared abstract.
5
Can have instance variables, static methods, and final methods just like any regular class.

Abstract vs Concrete Methods

Abstract Method Concrete Method
Has a body? No: ends with a semicolon Yes: has { } with code
Keyword abstract required No special keyword
Subclass duty Must override Can override or inherit
Purpose Define a contract Provide shared logic

Abstract Class vs Interface

Abstract Class Interface
Inheritance Single (extends one) Multiple (implements many)
Constructors Yes No
Instance fields Yes Only static final
Concrete methods Yes Only default (Java 8+)
Best for IS-A with shared state CAN-DO capability

How to Design with Abstract Classes

1
Identify shared behavior. What methods and fields do your related classes have in common?
2
Create the abstract class. Declare it with abstract and add shared fields and concrete methods.
3
Declare abstract methods for behavior that varies between subclasses. Give them no body.
4
Extend and override. Each subclass extends the abstract class and provides its own implementation of every abstract method.
5
Use the abstract type. Declare variables as the abstract type for polymorphism: Shape s = new Circle(5);

Example 1: Shape Hierarchy

public abstract class Shape { private String color; public Shape(String color) { this.color = color; } public String getColor() { return color; } public abstract double area(); }

Extending Shape: Subclasses

public 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 Rectangle extends Shape { private double w, h; public Rectangle(String color, double w, double h) { super(color); this.w = w; this.h = h; } @Override public double area() { return w * h; } }

Example 2: Animal Hierarchy

public abstract class Animal { private String name; public Animal(String name) { this.name = name; } public String getName() { return name; } public abstract String speak(); public void introduce() { System.out.println(name + " says: " + speak()); } }

Subclasses of Animal

public class Dog extends Animal { public Dog(String name) { super(name); } @Override public String speak() { return "Woof!"; } } public class Cat extends Animal { public Cat(String name) { super(name); } @Override public String speak() { return "Meow!"; } } // Usage with polymorphism: Animal a = new Dog("Rex"); a.introduce(); // "Rex says: Woof!"

Example 3: Constructors and Fields

public abstract class Vehicle { protected int speed; protected String fuel; public Vehicle(int speed, String fuel) { this.speed = speed; this.fuel = fuel; } public abstract double efficiency(); public String summary() { return fuel + " vehicle, " + speed + " mph, eff=" + efficiency(); } } public class ElectricCar extends Vehicle { private double batteryKWh; public ElectricCar(int speed, double kWh) { super(speed, "Electric"); this.batteryKWh = kWh; } @Override public double efficiency() { return batteryKWh / speed; } }

Polymorphism in Action

Shape[] shapes = { ... } for (Shape s : shapes) s.area() Circle
\( \text{radius} = 5 \)
\( \text{area()} \approx 78.54 \)
Rectangle
\( 4 \times 6 \)
\( \text{area()} = 24.0 \)
Triangle
\( b=8, h=3 \)
\( \text{area()} = 12.0 \)

Common Mistakes

✕ Instantiating Abstract Class
Writing new Shape() will not compile. You must create instances of concrete subclasses only.
✕ Forgetting to Override
If a subclass does not implement all abstract methods, it must also be declared abstract or it won't compile.
✕ Missing super() Call
If the abstract class has a parameterized constructor, the subclass must call super(...) in its own constructor.

Summary: Abstract Class Checklist

Feature Abstract Class Can...
Be instantiated directly No
Have constructors Yes
Have instance variables Yes
Have abstract methods Yes (no body)
Have concrete methods Yes (with body)
Be extended by one class Yes (single inheritance)
Use polymorphism Yes (declare as abstract type)

Question 1 of 5

Which of the following statements about abstract classes in Java is true?

Question 2 of 5

What happens if Parrot does not override the sound() method?

Question 3 of 5

Consider: Shape s = new Circle("blue", 3);
What does s.area() return?

Question 4 of 5

What is the primary advantage of an abstract class over an interface in Java?

Question 5 of 5

What is the result of this code?
Appliance a = new Blender(); a.turnOn(); a.operate();