Inheritance lets one class build on another: a Circle is a Shape, so it gets the shape’s fields and methods and adds its own. Polymorphism lets you treat different subclasses the same way: call show() on any shape and each computes its own area. This lesson covers extends, super, overriding, abstract classes and interfaces.
In this lesson you will learn
- How
extendsandsuperwork - Method overriding and
@Override - Abstract classes and abstract methods
- Interfaces and
implements - Runtime polymorphism and
instanceof
Shapes: Inheritance, Abstract Classes and Interfaces
abstract class Shape {
String name;
Shape(String name) { this.name = name; }
abstract double area(); // every subclass must provide it
void show() { System.out.printf("%s area = %.2f%n", name, area()); }
}
class Rectangle extends Shape {
double w, h;
Rectangle(double w, double h) { super("Rectangle"); this.w = w; this.h = h; }
@Override double area() { return w * h; }
}
class Circle extends Shape {
double r;
Circle(double r) { super("Circle"); this.r = r; }
@Override double area() { return Math.PI * r * r; }
}
interface Drawable {
void draw();
}
class Square extends Rectangle implements Drawable {
Square(double side) { super(side, side); name = "Square"; }
public void draw() { System.out.println("Drawing a square"); }
}
public class Main {
public static void main(String[] args) {
Shape[] shapes = {new Rectangle(3, 4), new Circle(1), new Square(2)};
for (Shape s : shapes) s.show(); // polymorphism
Square sq = new Square(5);
sq.draw();
System.out.println(sq instanceof Shape);
System.out.println(sq instanceof Drawable);
}
}
Output
Rectangle area = 12.00
Circle area = 3.14
Square area = 4.00
Drawing a square
true
true
abstract class Shapecannot be created withnew. Itsabstract double area();has no body, so every concrete subclass must provide one.class Rectangle extends Shapeinheritsnameandshow().super("Rectangle")calls the parent constructor and must be the first line.- Overriding: each subclass gives its own
area().@Overridemakes the compiler report typos in the method signature. Square extends Rectangleis multilevel inheritance; it reuses the rectangle’s area formula.interface Drawablelists methods a class promises to provide. A class extends one class but canimplementsmany interfaces.- Polymorphism: the loop holds everything as
Shape, yetshow()runs the rightarea()for each object, decided at runtime.
Abstract Class vs Interface
| Abstract class | Interface | |
|---|---|---|
| Keyword | extends | implements |
| How many per class | One | Many |
| Fields | Any | Constants only |
| Constructors | Yes | No |
| Best for | Closely related classes sharing code | A capability unrelated classes can share (Comparable, Runnable) |
Common Mistakes
- Calling
super(...)anywhere but the first line of a constructor. - Misspelling an overriding method, which silently creates a new method.
@Overridecatches this. - Trying to extend two classes. Java allows one parent class; use interfaces for the rest.
- Using inheritance for “has-a” relationships. A car has an engine; make it a field.
Practice Exercises
Exercise 1. Add a Triangle subclass of Shape with base and height.
Show answer
class Triangle extends Shape {
double b, h;
Triangle(double b, double h) { super("Triangle"); this.b = b; this.h = h; }
@Override double area() { return 0.5 * b * h; }
}
Exercise 2. Why can new Shape("x") not compile?
Show answer
Shape is abstract; it has an area() method with no body, so Java will not create a Shape directly. Create a concrete subclass instead.
Frequently Asked Questions
Does Java support multiple inheritance?
Not for classes: a class has exactly one parent. It can implement any number of interfaces, which gives most of the benefit without the ambiguity problems.
What is the difference between overloading and overriding?
Overloading is several methods with the same name but different parameters in one class, chosen at compile time. Overriding is a subclass replacing a parent method with the same signature, chosen at runtime.
Summary
extendsinherits from one parent;supercalls the parent.- Override methods to change behaviour, and mark them
@Override. - Abstract classes share code; interfaces define capabilities.
- Polymorphism runs the right method for each object at runtime.
Next: Java Exception Handling.
