When you create a child object, the parent portion of that object needs to be initialized too. You call the parent's constructor using super() with any required arguments.
public class Animal {
String name;
Animal(String name) {
this.name = name;
}
}
public class Dog extends Animal {
String breed;
Dog(String name, String breed) {
super(name);
this.breed = breed;
}
}
The super(name) call must be the first statement in the child constructor. If you put any code before it, the compiler throws an error.
If the parent has a no-argument constructor, Java inserts super() automatically. But if the parent only has a parameterized constructor, you must call super(...) yourself. Forgetting this is one of the most common compilation errors in inheritance.