</>
Skip to content
Java lessons (30/47)

Java — Inheritance

Basic inheritance

class Animal {
    String name;

    void eat() {
        System.out.println(name + " is eating");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println(name + " is barking");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.name = "Rex";
        dog.eat();   // Inherited from Animal
        dog.bark();  // Dog-specific
    }
}

Method overriding

class Animal {
    void speak() {
        System.out.println("The animal speaks");
    }
}

class Dog extends Animal {
    @Override
    void speak() {
        System.out.println("The dog barks");
    }
}

class Cat extends Animal {
    @Override
    void speak() {
        System.out.println("The cat meows");
    }
}

super keyword

class Person {
    String name;

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

    void greet() {
        System.out.println("Hello, I'm " + name);
    }
}

class Employee extends Person {
    String company;

    Employee(String name, String company) {
        super(name);
        this.company = company;
    }

    @Override
    void greet() {
        super.greet();
        System.out.println("I work at " + company);
    }
}

Constructors

class Base {
    Base() {
        System.out.println("Base constructor");
    }
}

class Derived extends Base {
    Derived() {
        super(); // Must be first statement
        System.out.println("Derived constructor");
    }
}

Protected access

class Parent {
    protected int x = 10;
    private int y = 20;
}

class Child extends Parent {
    void show() {
        System.out.println(x);  // OK: protected
        // System.out.println(y); // Error: private
    }
}

Final classes and methods

final class Immutable {
    private final int value;

    Immutable(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

// class Child extends Immutable {} // Error: cannot extend final class

class Parent {
    final void show() {
        System.out.println("Cannot override");
    }
}

class Child extends Parent {
    // void show() {} // Error: cannot override final method
}

Object class

class Person {
    String name;

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

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Person other = (Person) obj;
        return name.equals(other.name);
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "'}";
    }
}

Mini Practice

Write Java code that:

  1. Creates a parent class and child class
  2. Overrides a method with @Override
  3. Uses super to call parent constructor
  4. Demonstrates final class and methods

Up Next

In the next lesson, you'll learn about Interfaces — defining contracts in Java.

Related Topics

Frequently Asked Questions about Inheritance

What is Inheritance in Java?

Inheritance is a fundamental concept in Java. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Inheritance?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Inheritance.

Why is Inheritance important in Java?

Inheritance is essential for Java development. Understanding this concept will help you write better code and solve real-world problems more effectively.