Reading history

Java Inheritance, Polymorphism, Abstract Classes, Interfaces

3 min read 0 views

Learn Java inheritance and polymorphism: extends, super, overriding, abstract classes, interfaces, implements and runtime polymorphism.

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 extends and super work
  • 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 Shape cannot be created with new. Its abstract double area(); has no body, so every concrete subclass must provide one.
  • class Rectangle extends Shape inherits name and show(). super("Rectangle") calls the parent constructor and must be the first line.
  • Overriding: each subclass gives its own area(). @Override makes the compiler report typos in the method signature.
  • Square extends Rectangle is multilevel inheritance; it reuses the rectangle’s area formula.
  • interface Drawable lists methods a class promises to provide. A class extends one class but can implements many interfaces.
  • Polymorphism: the loop holds everything as Shape, yet show() runs the right area() for each object, decided at runtime.

Abstract Class vs Interface

Abstract classInterface
Keywordextendsimplements
How many per classOneMany
FieldsAnyConstants only
ConstructorsYesNo
Best forClosely related classes sharing codeA capability unrelated classes can share (Comparable, Runnable)

Common Mistakes

  1. Calling super(...) anywhere but the first line of a constructor.
  2. Misspelling an overriding method, which silently creates a new method. @Override catches this.
  3. Trying to extend two classes. Java allows one parent class; use interfaces for the rest.
  4. 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

  • extends inherits from one parent; super calls 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.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *