Skip to lesson

Common mistakes and practice

A review of this module. Skip it if you feel confident; the next module does not need it.

Fix the mistakes

The slips that cost marks in this module. Spot each one, then check the fix.

Most inheritance bugs come from a few patterns: using what the child cannot reach, forgetting the parent's constructor, and copying what the child already has. Knowing them by sight makes every class family easier to build.

Mistakes and fixes at a glance

The most common inheritance compile error comes from a missing super(...). The parent has only Person(String name), so the hidden super(); in the child finds no constructor to call.

Example 1 · Forgetting super(...) when the parent needs a value

TeacherDemo.javaJava
class Person {    private String name;
    Person(String name) {        this.name = name;    }}
class Teacher extends Person {    private String subject;
    Teacher(String name, String subject) {        this.subject = subject;    }}
public class TeacherDemo {    public static void main(String[] args) {        new Teacher("Mrs. Iyer", "Physics");    }}
Sample compiler output
TeacherDemo.java:12: error: constructor Person in class Person cannot be applied to given types;
    Teacher(String name, String subject) {
                                         ^
  required: String
  found:    no arguments
  reason: actual and formal argument lists differ in length
1 error

What this code does

  • Teacher's constructor has no super(...), so Java adds super();, and Person() does not exist.
  • Fix: super(name); as the first line, then this.subject = subject;. Adding an empty Person() would allow people with no name.

Review: Constructor order (opens in a new tab). The other common mistakes, each with what goes wrong and the fix:

Mistakes that compile

Some inheritance bugs give no error at all. The worst is declaring an inherited field again in the child: the object then has two name fields, and the parent's methods read the other one.

Example 2 · Bug: the child declares the parent's field again

DuplicateFieldDemo.javaJava
class Person {    String name;
    void greet() {        System.out.println("Hello, " + name);    }}
class Student extends Person {    String name;
    Student(String name) {        this.name = name;    }}
public class DuplicateFieldDemo {    public static void main(String[] args) {        Student s = new Student("Aarav");        s.greet();        System.out.println(s.name);    }}
Output
Hello, null
Aarav

What this code does

  • The Student object has two name fields. The constructor sets the child's copy.
  • greet() is written in Person, so it reads Person's copy, which is still null.

Example 3 · The fix: one field, set through super(...)

OneFieldDemo.javaJava
class Person {    private String name;
    Person(String name) {        this.name = name;    }
    void greet() {        System.out.println("Hello, " + name);    }}
class Student extends Person {    Student(String name) {        super(name);    }}
public class OneFieldDemo {    public static void main(String[] args) {        Student s = new Student("Aarav");        s.greet();    }}
Output
Hello, Aarav

What this code does

  • The field lives only in Person, and Person's constructor sets it. There is one name, so greet() reads the value that was stored.
  • Because the field is private, Student cannot touch it directly. Its only job is to pass the value up with super(name).

Two more slips compile silently: a parent constructor that reads a value the child sets later sees the default 0 or null (the trace below shows the order), and a custom exception without super(message) makes getMessage() return null.

Spot the bug

Each snippet below looks almost right. Read what it should do, tap the wrong line, then check the fix. Ask three questions: is this really IS-A, does every member exist where it is used, and does every constructor reach a real parent constructor?

Find the bug

Find the inheritance bug

Select the line that is wrong.

new Doctor("Dr. Arjun").show() should print Dr. Arjun.

Round 1 of 5

When in doubt, trace the constructors

A program with starting values and constructors at two levels prints numbers you did not expect. Trace it: the parent part is built first, field values before the constructor body, then the child part the same way.

Trace the code

Trace field values and constructors

class Account {
int balance = 100;
Account() {
System.out.println("Account: " + balance);
balance = balance * 2;
}
}
class Savings extends Account {
int bonus = balance + 50;
Savings(int extra) {
balance = balance + extra;
System.out.println("Savings: " + balance + ", bonus " + bonus);
}
}
public class InitTrace {
public static void main(String[] args) {
Savings s = new Savings(30);
System.out.println(s.balance + " " + s.bonus);
}
}

Variables

extra
30
balance
0
bonus
0

Output

(nothing printed yet)

Line 13new Savings(30) makes one object with both fields at 0 and calls Savings(30). Its hidden super() runs first.

Step 1 of 8

Finish up

Mark complete & continue

Next: Must-do problems: Inheritance