Services Work Learn About Contact
0/10
Lesson 8 of 10

Constructors & Inheritance

A constructor is a special method that runs when an object is created. It has the same name as the class. Inheritance lets a class (child) extend another class (parent) and reuse its code.

JAVA
// === File: Animal.java ===
public class Animal {
  String name;

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

  void speak() {
    System.out.println("Some sound");
  }
}

// === File: Dog.java ===
public class Dog extends Animal {
  Dog(String name) {
    super(name);
  }

  void speak() {
    System.out.println("Woof!");
  }
}
🧠

Quick Quiz

Answer correctly to unlock the next lesson.

Support the mission

This learning platform is 100% free: no ads, no tracking, no paywalls. If it helped you learn something useful, you can support future lessons or donate to Doctors Without Borders, which provides emergency medical care in crisis zones worldwide.

🎉

You completed Java!

You finished all 10 lessons and quizzes. You now know the basics of Java.