VisualStudioTutor.com Java Tutorial All Lessons

Lesson 16 of 20

Inheritance and Polymorphism

Reuse common behavior with inheritance and write flexible code that works through shared parent types.

Learning objectives

  • Create a subclass with extends.
  • Call a superclass constructor with super.
  • Override inherited methods.
  • Use a parent reference to work with different child objects.
  • Explain runtime polymorphism.

1. Inheritance

Inheritance models an is-a relationship. If Student and Lecturer are both kinds of Person, shared data can live in the parent class.

Example: base class

public class Person {
    protected String name;

    public Person(String name) {
        this.name = name;
    }

    public void describe() {
        System.out.println("Person: " + name);
    }
}

Example: subclass

public class Student extends Person {
    private String course;

    public Student(String name, String course) {
        super(name);
        this.course = course;
    }

    @Override
    public void describe() {
        System.out.println("Student: " + name + ", Course: " + course);
    }
}

super(name) calls the parent constructor. @Override asks the compiler to verify that the method truly overrides an inherited method.

2. Polymorphism

Polymorphism means a parent-type reference can refer to different subclass objects. When an overridden method is called, Java selects the implementation belonging to the actual object at runtime.

Example: one type, different behavior

public class Lecturer extends Person {
    private String department;

    public Lecturer(String name, String department) {
        super(name);
        this.department = department;
    }

    @Override
    public void describe() {
        System.out.println("Lecturer: " + name + ", Department: " + department);
    }
}

public class PeopleApp {
    public static void main(String[] args) {
        Person[] people = {
            new Student("Amina", "Computer Science"),
            new Lecturer("Dr Tan", "Computing")
        };

        for (Person person : people) {
            person.describe();
        }
    }
}

Expected output

Student: Amina, Course: Computer Science
Lecturer: Dr Tan, Department: Computing

Mini exercise

Add a third subclass named Administrator with a role field. Override describe(), add an Administrator object to the array, and verify that the correct method runs automatically.

Common mistakes

ProblemWhy it happensHow to fix it
Forgetting super(...)The parent class requires constructor arguments.Call an appropriate superclass constructor first.
Method does not really overrideParameter types or method name differ.Use @Override so the compiler catches the mismatch.
Using inheritance only to reuse codeThe relationship may not be conceptually valid.Use inheritance when the subclass genuinely is a specialized form of the parent.

Summary

Inheritance shares common structure; polymorphism lets callers work through a common type while each object supplies its own behavior. Lesson 17 compares abstract classes with interfaces.