Object-Oriented Programming

Inheritance

Create new classes based on existing ones.

The extends Keyword

class Animal {
    void eat() {
        System.out.println("Eating...");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Woof!");
    }
}

// Dog inherits eat() from Animal
Dog d = new Dog();
d.eat();  // "Eating..."
d.bark(); // "Woof!"

super Keyword

Call parent class constructor or methods:

class Animal {
    String name;
    
    Animal(String name) {
        this.name = name;
    }
}

class Dog extends Animal {
    Dog(String name) {
        super(name); // Call parent constructor
    }
}
Visualizer
$ java Main.java
Whiskers says: Meow!

Interactive Visualization

java
Loading...
Output
Run execution to see output...