Skip to main content

    Lesson 10 • Intermediate

    Java Inheritance

    Imagine you have Dog, Cat, and Bird classes. Each needs a name, age,eat(), and sleep(). Without inheritance, you'd copy-paste the same code three times. Inheritance lets you define shared behavior once in a parent class (Animal), and every child class automatically gets it. Write once, reuse everywhere.

    What You'll Learn

    • ✅ What inheritance is and why it eliminates code duplication
    • ✅ The extends keyword — creating child classes
    • ✅ The super keyword — calling parent constructors and methods
    • ✅ Method overriding — replacing parent behavior in child classes
    • @Override annotation — safety net for overriding
    • ✅ Polymorphism — same method name, different behavior per type
    • instanceof — checking an object's type at runtime
    • ✅ Inheritance vs Composition — the critical design decision

    💡 Real-World Analogy: Family Tree

    Think of a family tree. A child inherits traits (eye color, height) from their parents, but can also develop unique traits of their own. Similarly, a child class inherits fields and methods from a parent class, but can add new ones or override inherited ones with different behavior.

    1️⃣ The Problem Inheritance Solves

    Without inheritance, you'd duplicate code across similar classes:

    // ❌ WITHOUT inheritance — massive code duplication!
    class Dog {
        String name;      // duplicated
        int age;          // duplicated
        void eat() { }    // duplicated
        void sleep() { }  // duplicated
        void bark() { }   // unique to Dog
    }
    
    class Cat {
        String name;      // duplicated!
        int age;          // duplicated!
        void eat() { }    // duplicated!
        void sleep() { }  // duplicated!
        void purr() { }   // unique to Cat
    }
    
    // ✅ WITH inheritance — shared code written ONCE
    class Animal {
        String name;
        int age;
        void eat() { System.out.println(name + " eats"); }
        void sleep() { System.out.println(name + " sleeps"); }
    }
    
    class Dog extends Animal {
        void bark() { System.out.println(name + " barks!"); }
    }
    
    class Cat extends Animal {
        void purr() { System.out.println(name + " purrs..."); }
    }

    🔑 The extends keyword: class Dog extends Animal means "Dog is a type of Animal and inherits all its fields and methods." Dog automatically gets name, age, eat(), and sleep() without writing them again.

    2️⃣ The super Keyword — Talking to the Parent

    super refers to the parent class. You use it in two main places:

    class Animal {
        String name;
        int age;
    
        Animal(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        void speak() {
            System.out.println(name + " makes a sound");
        }
    }
    
    class Dog extends Animal {
        String breed;
    
        // 1. super() in constructor — MUST be first line!
        Dog(String name, int age, String breed) {
            super(name, age);    // Calls Animal's constructor
            this.breed = breed;  // Then set Dog-specific field
        }
    
        // 2. super.method() — call parent's version
        @Override
        void speak() {
            super.speak();  // "Rex makes a sound"
            System.out.println(name + " barks! 🐕");
        }
    }

    ⚠️ Critical rule: If the parent class has no default (no-argument) constructor, you must call super(...) as the very first statement in the child's constructor. The compiler will refuse to compile otherwise.

    3️⃣ Method Overriding — Customizing Inherited Behavior

    A child class can override a parent's method to provide its own implementation. The method signature (name, parameters, return type) must match exactly. Always use the @Override annotation for safety.

    class Animal {
        void speak() {
            System.out.println("Some generic animal sound");
        }
    }
    
    class Dog extends Animal {
        @Override  // Tells compiler: "I'm intentionally replacing parent's version"
        void speak() {
            System.out.println("Woof! 🐕");
        }
    }
    
    class Cat extends Animal {
        @Override
        void speak() {
            System.out.println("Meow! 🐱");
        }
    }
    
    // Each type speaks differently:
    new Animal().speak();  // "Some generic animal sound"
    new Dog().speak();     // "Woof! 🐕"
    new Cat().speak();     // "Meow! 🐱"

    💡 Why @Override? Without it, a typo like speek() would create a new method instead of overriding speak() — a silent, hard-to-find bug. @Override tells the compiler to verify the parent actually has this method.

    Try It: Inheritance Basics
    // Parent class — shared fields and behavior live here once.
    class Animal {
        String name;
        int age;
    
        Animal(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        String eat()   { return name + " is eating"; }
        String sleep() { return name + " is sleeping"; }
        String speak() { return name + " makes a sound"; }
        String info()  { return name + " (age " + age + ")"; }
    }
    
    // Child classes inherit everything from Animal and add/override behavior.
    class Dog extends Animal {
        String breed;
        Dog(String name, int age, String breed) {
            super(name, age);          // call the parent constructor first
            this.breed = breed;
        }
        @Override String speak() { return name + " barks!"; }   // override
        String fetch() { return name + " fetches the ball!"; }  // new method
    }
    
    class Cat extends Animal {
        Cat(String name, int age) { super(name, age); }
        @Override String speak() { return name + " meows!"; }
        String purr() { return name + " purrs..."; }
    }
    
    class Bird extends Animal {
        Bird(String name, int age) { super(name, age); }
        @Override String speak() { return name + " chirps!"; }
        String fly() { return name + " is flying!"; }
    }
    
    public class Main {
        public static void main(String[] args) {
            Dog dog = new Dog("Rex", 5, "Golden Retriever");
            Cat cat = new Cat("Whiskers", 3);
            Bird bird = new Bird("Tweety", 1);
    
            System.out.println("1. INDIVIDUAL OBJECTS:");
            System.out.println("   " + dog.info() + " - " + dog.breed);
            System.out.println("   " + dog.speak());
            System.out.println("   " + dog.eat());    // inherited from Animal
            System.out.println("   " + dog.fetch());  // Dog-only method
            System.out.println("   " + cat.speak());
            System.out.println("   " + cat.purr());    // Cat-only method
            System.out.println("   " + bird.speak());
            System.out.println("   " + bird.fly());     // Bird-only method
    
            System.out.println("2. INHERITED METHODS (eat):");
            Animal[] animals = { dog, cat, bird };
            for (Animal a : animals) System.out.println("   " + a.eat());
        }
    }
    Output
    1. INDIVIDUAL OBJECTS:
       Rex (age 5) - Golden Retriever
       Rex barks!
       Rex is eating
       Rex fetches the ball!
       Whiskers meows!
       Whiskers purrs...
       Tweety chirps!
       Tweety is flying!
    2. INHERITED METHODS (eat):
       Rex is eating
       Whiskers is eating
       Tweety is eating
    This is real code — run it for free atonecompiler.com/javaor in your own editor.

    4️⃣ Polymorphism — "Many Forms"

    Polymorphism is the most powerful concept in OOP. It means a parent type variable can hold any child type object, and method calls automatically use the child's version. Think of it like a universal TV remote — the "power" button does something different on every TV brand.

    // The parent type can hold ANY child object
    Animal myPet = new Dog("Rex", 5, "Lab");
    myPet.speak();  // "Woof! 🐕" — calls Dog's version!
    myPet.eat();    // "Rex is eating" — inherited from Animal
    
    // Process different animals uniformly
    Animal[] zoo = {
        new Dog("Rex", 5, "Lab"),
        new Cat("Whiskers", 3),
        new Bird("Tweety", 1)
    };
    
    for (Animal a : zoo) {
        a.speak();  // Each animal speaks differently!
    }
    // → "Rex barks!" / "Whiskers meows!" / "Tweety chirps!"

    🔑 Why this is powerful: You can write methods that accept Animal and they automatically work with Dog, Cat, Bird, and any future animal class you create. You don't need to change the method — it just works. This is the foundation of extensible software.

    5️⃣ instanceof — Type Checking at Runtime

    Sometimes you have a parent type variable but need to know the actual type at runtime. Use instanceof:

    Animal pet = new Dog("Rex", 5, "Lab");
    
    pet instanceof Dog     // true  — it IS a Dog
    pet instanceof Animal  // true  — it's also an Animal
    pet instanceof Cat     // false — it's NOT a Cat
    
    // Useful for type-specific actions:
    if (pet instanceof Dog) {
        Dog d = (Dog) pet;  // "Casting" — now we can call Dog methods
        d.fetch();           // This method only exists on Dog
    }
    
    // Java 16+ pattern matching (cleaner):
    if (pet instanceof Dog d) {
        d.fetch();  // No separate cast needed!
    }
    Try It: Polymorphism in Action
    abstract class Shape {
        abstract double area();
        String describe() {
            return getClass().getSimpleName()
                + " -> area = " + String.format("%.2f", area());
        }
    }
    
    class Circle extends Shape {
        double radius;
        Circle(double radius) { this.radius = radius; }
        @Override double area() { return Math.PI * radius * radius; }
    }
    
    class Rectangle extends Shape {
        double width, height;
        Rectangle(double width, double height) { this.width = width; this.height = height; }
        @Override double area() { return width * height; }
    }
    
    class Triangle extends Shape {
        double base, height;
        Triangle(double base, double height) { this.base = base; this.height = height; }
        @Override double area() { return 0.5 * base * height; }
    }
    
    public class Main {
        public static void main(String[] args) {
            // A Shape[] holds ANY subclass — that's polymorphism.
            Shape[] shapes = {
                new Circle(5),
                new Rectangle(10, 4),
                new Triangle(8, 6),
                new Circle(3)
            };
    
            System.out.println("1. POLYMORPHISM (same area() method):");
            Shape largest = shapes[0];
            double total = 0;
            for (Shape s : shapes) {
                System.out.println("   " + s.describe());
                total += s.area();
                if (s.area() > largest.area()) largest = s;
            }
    
            System.out.println("2. LARGEST: " + largest.describe());
            System.out.println("3. TOTAL AREA: " + String.format("%.2f", total));
    
            System.out.println("4. TYPE CHECKING (instanceof):");
            for (Shape s : shapes) {
                if (s instanceof Circle c) {   // Java 16+ pattern matching
                    System.out.println("   Circle with radius " + c.radius);
                }
            }
        }
    }
    Output
    1. POLYMORPHISM (same area() method):
       Circle -> area = 78.54
       Rectangle -> area = 40.00
       Triangle -> area = 24.00
       Circle -> area = 28.27
    2. LARGEST: Circle -> area = 78.54
    3. TOTAL AREA: 170.81
    4. TYPE CHECKING (instanceof):
       Circle with radius 5.0
       Circle with radius 3.0
    This is real code — run it for free atonecompiler.com/javaor in your own editor.

    6️⃣ Inheritance vs Composition

    This is one of the most important design decisions you'll make. The test is simple:

    Inheritance ("is-a") ✅

    Dog is an Animal. Circle is a Shape.

    class Dog extends Animal { }
    class Circle extends Shape { }

    Composition ("has-a") ✅

    Car has an Engine. House has a Kitchen.

    class Car {
        private Engine engine; // field
    }

    ❌ Bad inheritance: Stack extends ArrayList — a Stack is NOT a type of ArrayList. It just happens to use a list internally. Use composition instead.

    💡 Pro rule: "Favor composition over inheritance." Deep inheritance hierarchies (5+ levels) become rigid and hard to change. Use 1-2 levels of inheritance + interfaces for flexibility.

    7️⃣ Access Modifiers in Inheritance

    Not all parent members are accessible to child classes. Here's what each access modifier allows:

    ModifierSame ClassSubclassOther Classes
    public
    protected
    (default)Same pkgSame pkg
    private

    Use protected for fields/methods that subclasses need but outsiders shouldn't access. It's the sweet spot for inheritance.

    Try It: Employee Hierarchy
    class Employee {
        String name;
        int baseSalary;   // monthly
        Employee(String name, int baseSalary) { this.name = name; this.baseSalary = baseSalary; }
    
        int getAnnualSalary() { return baseSalary * 12; }
        String getRole() { return "Employee"; }
    
        @Override public String toString() {
            return String.format("%-10s %-12s $%d", name, getRole(), getAnnualSalary());
        }
    }
    
    class Manager extends Employee {
        int bonus;
        Manager(String name, int salary, int bonus) { super(name, salary); this.bonus = bonus; }
        @Override int getAnnualSalary() { return super.getAnnualSalary() + bonus; }
        @Override String getRole() { return "Manager"; }
    }
    
    class Developer extends Employee {
        String language;
        Developer(String name, int salary, String language) { super(name, salary); this.language = language; }
        @Override String getRole() { return "Developer"; }
        String code() { return name + " is coding in " + language; }
    }
    
    class Intern extends Employee {
        Intern(String name, int salary) { super(name, salary); }
        @Override int getAnnualSalary() { return super.getAnnualSalary() / 2; }  // part-time
        @Override String getRole() { return "Intern"; }
    }
    
    public class Main {
        public static void main(String[] args) {
            Employee[] team = {
                new Manager("Alice", 8000, 24000),
                new Developer("Bob", 7500, "Java"),
                new Developer("Charlie", 7200, "Python"),
                new Intern("Eve", 3000)
            };
    
            System.out.println("1. TEAM ROSTER:");
            int totalPayroll = 0;
            Employee highest = team[0];
            for (Employee e : team) {
                System.out.println("   " + e);
                totalPayroll += e.getAnnualSalary();
                if (e.getAnnualSalary() > highest.getAnnualSalary()) highest = e;
            }
    
            System.out.println("2. TOTAL PAYROLL: $" + totalPayroll);
            System.out.println("3. HIGHEST EARNER: " + highest.name
                + " ($" + highest.getAnnualSalary() + ")");
    
            System.out.println("4. DEVELOPERS:");
            for (Employee e : team) {
                if (e instanceof Developer d) System.out.println("   " + d.code());
            }
        }
    }
    Output
    1. TEAM ROSTER:
       Alice      Manager      $120000
       Bob        Developer    $90000
       Charlie    Developer    $86400
       Eve        Intern       $18000
    2. TOTAL PAYROLL: $314400
    3. HIGHEST EARNER: Alice ($120000)
    4. DEVELOPERS:
       Bob is coding in Java
       Charlie is coding in Python
    This is real code — run it for free atonecompiler.com/javaor in your own editor.

    Common Mistakes

    • ❌ Forgetting super() in child constructor: If the parent has no default constructor, you MUST explicitly call super(...) as the first line.
    • ❌ Forgetting @Override: A typo like speek() creates a new method instead of overriding speak(). The annotation catches this at compile time.
    • ❌ Overusing inheritance: Don't use inheritance just for code reuse. Use it only for true "is-a" relationships. If it's "has-a", use composition.
    • ❌ Deep inheritance hierarchies: More than 3 levels deep becomes confusing and rigid. Keep it shallow.
    • ❌ Calling overridden methods in constructors: This can cause bugs because the child class isn't fully initialized yet when the parent constructor runs.

    Pro Tips

    💡 Always use @Override: It's free compile-time error checking. There's no reason not to use it.

    💡 Keep hierarchies shallow: 1-2 levels of inheritance is ideal. Beyond 3, consider interfaces + composition.

    💡 Program to interfaces, not implementations: Use Animal pet = new Dog() instead of Dog pet = new Dog() when possible — this makes your code more flexible.

    💡 Use protected wisely: It's better than public for fields that subclasses need, but consider if a getter method would be even better.

    💡 The Liskov Substitution Principle: A child class should work anywhere the parent class is expected. If a method expects Animal, passing a Dog should work correctly.

    📋 Quick Reference

    KeywordSyntaxPurpose
    extendsclass Dog extends AnimalInherit from parent
    super()super(name, age)Call parent constructor
    super.method()super.speak()Call parent's method version
    @Override@Override void speak()Override with safety check
    instanceofpet instanceof DogCheck object's actual type
    protectedprotected String name;Accessible by subclasses

    🎉 Lesson Complete!

    You now understand inheritance, method overriding, polymorphism, the super keyword, and when to choose inheritance vs composition! These concepts are the backbone of extensible, maintainable Java applications.

    Next up: Interfaces & Abstract Classes — define contracts and partial blueprints for maximum design flexibility.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service