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
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"); }}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 errorWhat this code does
Teacher's constructor has nosuper(...), so Java addssuper();, andPerson()does not exist.- Fix:
super(name);as the first line, thenthis.subject = subject;. Adding an emptyPerson()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:
name has private access in Person→ the child names a private parent field → use the parent's getter, or pass the value withsuper(...). Review: extends (opens in a new tab)call to super must be first statement in constructor→ child code runs beforesuper(...)→ movesuper(...)to the top. Review: super() (opens in a new tab)cannot find symbolon a member → it lives in a child or a sibling → use an object of the class that has it. Review: Multilevel and hierarchical (opens in a new tab)'{' expectedafterextends A, B, orcyclic inheritance→ one parent, one direction. Review: One parent only (opens in a new tab)cannot inherit from final Xoroverridden method is final→ do not extend or replace it; add a new method. Review: final methods and classes (opens in a new tab)unreported exception X, orgetMessage()returnsnull→ catch or declare it; callsuper(message). Review: Custom exceptions (opens in a new tab)Car extends Enginecompiles but fails the IS-A test → keep the engine in a field (HAS-A). Review: Why inheritance? (opens in a new tab)
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
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); }}Hello, null Aarav
What this code does
- The
Studentobject has twonamefields. The constructor sets the child's copy. greet()is written inPerson, so it readsPerson's copy, which is stillnull.
Example 3 · The fix: one field, set through super(...)
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(); }}Hello, Aarav
What this code does
- The field lives only in
Person, andPerson's constructor sets it. There is onename, sogreet()reads the value that was stored. - Because the field is
private,Studentcannot touch it directly. Its only job is to pass the value up withsuper(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 5When 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
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.
Finish up
Mark complete & continue
Next: Must-do problems: Inheritance