JOOPs Through Java
Challenge roadmap
0/30 Days Completed 0 Learning Days

Day 19 of 30 · Week 3 — OOPS Fundamentals

Inheritance

1 Learn

Inheritance lets one class reuse another class's fields and methods. The new class (child/subclass) extends the existing class (parent/superclass).

It models an "is-a" relationship: a Dog is an Animal. Java classes extend only one class directly.

Want more detail? Open the full lesson

2 See the Code

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

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

public class Zoo {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.name = "Bruno";
        d.eat();
        d.bark();
    }
}
  • Dog extends Animal, so Dog gets name and eat() for free.
  • Dog adds its own method bark().

Output

Bruno is eating
Bruno says Woof

3 Practice

  • Create Vehicle with start(), then Bike extends Vehicle with ringBell(). Call both.

Try it in your own Java setup or write it in the Practice Notebook.

4 Quick Check

  1. 1. Which keyword is used for class inheritance?

  2. 2. Inheritance models which relationship?

Step 5

Answer and check the Quick Check above to unlock this.