Problem overview
The starter code has a Dog class whose bark() prints Woof! I am <name>, and a Puppy class that extends Dog and overrides bark() with an empty body. The task is to fill that body so a puppy first does everything a dog does and then prints Yip! Said <name> as well.
The driver creates two puppies, Buddy and Rex, and a plain Dog named Max, and calls bark() on each. The expected output is:
Woof! I am Buddy
Yip! Said Buddy
Woof! I am Rex
Yip! Said Rex
Woof! I am Max
Each puppy prints two lines, the parent's and its own. The dog prints only the parent's line, because Dog.bark() is not changed.
Approach: extend the parent's method instead of replacing it
When a subclass overrides a method, the new body takes the place of the inherited one. If Puppy.bark() only printed Yip!, a puppy would never print the Woof! line:
class Puppy extends Dog {
public Puppy(String name) { super(name); }
@Override
public void bark() {
System.out.println("Yip! Said " + name);
}
}
With that body, new Puppy("Pip").bark() prints only Yip! Said Pip. Copying the parent's println into the puppy would fix the output, but it duplicates code: if Dog.bark() changes later, every copy has to change too.
The keyword super avoids the copy. Inside an instance method, super.bark() calls the parent class's version of bark on the current object. The method then continues with its own statements, so the result is the parent's behaviour plus something extra.
Step by step
- Call the parent first. Inside
Puppy.bark(), writesuper.bark();. This runsDog's body for the same object, so theWoof!line, with the puppy's own name, is printed. - Add the puppy's own line. Print
"Yip! Said "followed byname. The fieldnameis declaredpublicinDogand inherited, soPuppyreads it directly.
The order matters: the lesson asks for the parent's line before the puppy's, so the call to super.bark() comes first.
The solution in Java
class Dog {
public String name;
public Dog(String name) { this.name = name; }
public void bark() {
System.out.println("Woof! I am " + name);
}
}
class Puppy extends Dog {
public Puppy(String name) { super(name); }
@Override
public void bark() {
super.bark();
System.out.println("Yip! Said " + name);
}
}
class Solution {
public void mainFunction() {
Puppy buddy = new Puppy("Buddy");
buddy.bark();
Puppy rex = new Puppy("Rex");
rex.bark();
Dog dog = new Dog("Max");
dog.bark();
}
}
Details worth knowing:
super(name)andsuper.bark()are different things. The first is a constructor call. It may only appear as the first statement of a constructor and passesnameup soDog's constructor stores it. The second is an ordinary method call that can appear anywhere inside an instance method.- The names come from the object. Both lines use the same
namefield, soRexprintsRexin both. Nothing is hard-coded. @Overrideis a safety check. It makes the compiler report an error if no method with that name and parameters exists in the parent, which catches typos such asBark().superreaches one level up. It names the direct parent class. IfDoghad its own parent that also definedbark,super.bark()inPuppywould runDog's version, which could in turn call its parent's.- The object decides which
barkruns. WithDog d = new Puppy("Zed"); d.bark();the variable's type isDog, yet the output isWoof! I am Zedfollowed byYip! Said Zed, because the object is aPuppy.
Extending or replacing
Both choices are legitimate, and the right one depends on what the subclass is for. Extend, with super.method(), when the subclass is a refinement: it must keep everything the parent guarantees and add to it. Replace, by leaving super out, when the subclass behaves differently on purpose and the parent's steps would be wrong or redundant for it. In this lesson a puppy is still a dog that barks, so it extends.
A useful habit is to read super.bark() as "do the inherited thing here". If the parent's method later gains another statement, every subclass that calls super.bark() picks it up without any edit, which is exactly what a copied println would not do.
Edge cases and common mistakes
- Leaving out
super.bark(). The method then replaces the parent's behaviour: the output for a puppy would be onlyYip! Said Pip. - Calling
bark()withoutsuper.. InsidePuppy.bark(), a barebark()calls the same method again, which calls itself again, and the program ends with aStackOverflowError. The wordsuperis what sends the call up toDog. - Wrong order. Printing the puppy's line first and calling
super.bark()second is valid Java, but it printsYip! Said PipbeforeWoof! I am Pip, which doesn't match the expected output. - Changing
Dog. The task only touchesPuppy.bark(). The plainDognamedMaxmust still print a single line.
Testing
Besides the lesson's driver, these variations show the rules from the sections above. All three use a puppy named Pip or Zed:
- A
Puppywhosebark()prints its own line first and callssuper.bark()second printsYip! Said Pip, thenWoof! I am Pip. - A
Puppywhosebark()only prints its own line prints justYip! Said Pip. Dog d = new Puppy("Zed"); d.bark();printsWoof! I am Zedand thenYip! Said Zed.
Key takeaways
- Overriding replaces a method;
super.method()inside the override brings the parent's behaviour back in. - Put the
supercall where the parent's work should happen, before or after the subclass's own statements. - Use
super(...)in a constructor to pass values to the parent's constructor, andsuper.name(...)in a method to reuse the parent's method.
