Abstract Classes
Overview
An Abstract Class is a restricted blueprint. It is a class that is so generic or incomplete that it cannot be instantiated into an Object. You cannot write new Animal() if Animal is abstract.
Its sole purpose is to act as a strict Base Parent for other classes to extend. An abstract class can contain Abstract Methods (methods with no body, just a signature). Any child class that inherits from this abstract parent is violently forced by the compiler to provide the missing code for those abstract methods.
It is the perfect balance: It allows you to share actual code (normal methods and variables) among all children, while also enforcing strict contracts (abstract methods) that the children must uniquely implement.
Syntax
// 1. The Abstract Class (Cannot be instantiated!)
public abstract class Shape {
String color = "Red"; // Shared variable
// Normal method with a body (Shared by all children)
public void printColor() {
System.out.println("Color: " + color);
}
// ABSTRACT METHOD: No body! Just a signature ending in a semicolon.
// Forces all children to figure out how to calculate their own area.
public abstract double calculateArea();
}
// 2. The Concrete Child Class
public class Circle extends Shape {
double radius;
public Circle(double r) { this.radius = r; }
// The compiler FORCES us to write this method!
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
public class Main {
public static void main(String[] args) {
// Shape s = new Shape(); // ERROR! Cannot instantiate Abstract Class
Shape myShape = new Circle(5.0); // Polymorphism is totally valid!
System.out.println(myShape.calculateArea());
}
}Common Pitfalls
- Trying to make an abstract method
private. If an abstract method is private, the child class cannot see it. If the child class cannot see it, it cannot override it. This creates an impossible paradox and fails to compile. - Creating an abstract class with no abstract methods. While perfectly legal in Java, it usually indicates bad architecture. If a class has no abstract requirements, there is rarely a good reason to forcefully block instantiation.
Interview Questions
Yes! Even though you cannot use new AbstractClass(), the child classes still need to call super() to initialize the shared variables inherited from the abstract parent.
An Abstract class can have state (instance variables) and regular methods with bodies, but a child can only extend ONE abstract class. An Interface traditionally only has abstract methods and constants, but a child can implement UNLIMITED interfaces.
Real-World Example
In game development, you might have an abstract Enemy class. It manages shared logic like health and takeDamage(). But the attack() method is marked abstract, forcing the Zombie class to implement a melee attack, and the Sniper class to implement a ranged attack.
public abstract class Enemy {
int hp = 100;
public void takeDamage(int dmg) { hp -= dmg; }
// Every specific enemy MUST define how it attacks!
public abstract void attack();
}Check Your Knowledge
Test your understanding of Abstract Classes with these quick questions.