Problem overview
The starter code has a class Dog whose bark() prints Woof! I am <name>, and a subclass Puppy that overrides bark() with an empty body. Your job is to fill in that body so a puppy first does everything a dog does, then prints its own line, Yip! Said <name>.
The driver creates two puppies, Buddy and Rex, and a plain Dog called Max. The expected output is two lines for each puppy and one line for the dog:
Woof! I am Buddy
Yip! Said Buddy
Woof! I am Rex
Yip! Said Rex
Woof! I am Max
Approach: extend the parent's method, don't replace it
When a subclass overrides a method, its version takes over completely. The parent's body doesn't run unless the new body asks for it. Other languages call it "base"; Scala uses super, which stands for the parent class, and super.bark() runs Dog's version of bark on the current object.
The difference is easy to see with two subclasses of the same Dog. The first overrides bark and prints only its own line. The second calls super.bark() twice:
class Dog(val name: String) {
def bark(): Unit = println(s"Woof! I am $name")
}
class Quiet(name: String) extends Dog(name) {
override def bark(): Unit = println(s"Yip! Said $name")
}
class Loud(name: String) extends Dog(name) {
override def bark(): Unit = { super.bark(); super.bark() }
}
Calling bark() on Quiet("Buddy") prints only Yip! Said Buddy: the parent's line is gone. Calling it on Loud("Rex") prints Woof! I am Rex twice, because each super.bark() runs the parent's code.
That is the whole idea of the lesson. A Puppy must keep the parent's behaviour and add to it, so the body needs the parent call and then one more line.
Step by step
- Call
super.bark()first. This producesWoof! I am Buddy, exactly the line a plain dog prints. Without this call the puppy would replace the dog's bark instead of extending it. - Print the puppy's own line. Use an
sstring,s"Yip! Said $name", so the current object's name is inserted. Nothing is hard-coded, which is whyRexgets its own name in the output.
The order matters here: the expected output has the parent's line before the puppy's line, so the parent call comes first.
The solution in Scala
class Dog(val name: String) {
def bark(): Unit = {
println(s"Woof! I am $name")
}
}
class Puppy(name: String) extends Dog(name) {
override def bark(): Unit = {
super.bark()
println(s"Yip! Said $name")
}
}
class Solution {
def mainFunction(): Unit = {
val buddy = Puppy("Buddy")
buddy.bark()
val rex = Puppy("Rex")
rex.bark()
val dog = Dog("Max")
dog.bark()
}
}
Details worth knowing
overrideis required. Redefining a method that the parent already has without the keyword is a compile error: the compiler reports that the method needs theoverridemodifier. The keyword also protects you from typos:override def bak()in a class whose parent has nobakfails with "method bak overrides nothing".extends Dog(name)passes the constructor argument up.Puppy's parameternameis handed toDog's constructor, whereval namemakes it a field. InsidePuppy, the plain parameternameis also available in method bodies.- The dog is untouched. The last driver line calls
bark()on a plainDog, which prints onlyWoof! I am Max. Overriding inPuppychanges nothing forDogobjects.
Edge cases and common mistakes
- Forgetting
super.bark(). The output then has only theYip!lines, and theWoof!line is missing for both puppies. - Calling
bark()instead ofsuper.bark(). Inside the override, a plainbark()calls the same method again. It never reaches the parent: the method calls itself until the program fails with aStackOverflowError.super.is what jumps to the parent's version. - Wrong order. Printing the puppy's line first gives the correct lines in the wrong order, which fails the output check.
- Editing
Doginstead ofPuppy. Adding theYip!line toDog.bark()would make the plain dog print it too, and the last line of the expected output would no longer match. - Missing the
sprefix. Without it,"Yip! Said $name"prints the text$nameliterally instead of the name.
Testing
Besides the lesson's driver, try the Loud and Quiet classes from the approach section. Quiet("Buddy").bark() prints Yip! Said Buddy, and Loud("Rex").bark() prints Woof! I am Rex twice.
Now store a subclass object in a variable typed as the parent:
val d: Dog = Quiet("Zed")
d.bark()
The output is Yip! Said Zed. The variable's declared type is Dog, but the method that runs belongs to the object, so the override wins. This is called dynamic dispatch, and it is what makes super useful: callers can use any Dog without knowing which subclass they hold, and each subclass decides whether to extend or replace the parent's behaviour.
Chains of parents
super always means the nearest parent, and that parent's own method may call its parent in turn. With three levels, each override adds one line on top of the previous one:
class Animal { def describe(): Unit = println("An animal") }
class Dog2 extends Animal {
override def describe(): Unit = { super.describe(); println("A dog") }
}
class Puppy2 extends Dog2 {
override def describe(): Unit = { super.describe(); println("A puppy") }
}
Calling describe() on a Puppy2 prints three lines:
An animal
A dog
A puppy
Puppy2 asks Dog2, which asks Animal. The innermost call finishes first, so the lines come out from the oldest ancestor down to the newest subclass. Putting the super call last instead would reverse that order, which shows why the position of the call is a real design choice and not a detail.
Key takeaways
- Overriding replaces the parent's method;
super.method()brings the parent's behaviour back in. - Put the
supercall where the parent's work should happen: before, after or between your own code. - Scala requires
overridewhen you redefine an inherited method.
