Problem overview
A subclass can override a method of its parent, and an override replaces the parent's version. Often that is not what you want. You want to keep everything the parent does and add a little more. Kotlin gives you super for that: inside an overriding method, super.bark() runs the parent's bark on the same object.
The lesson gives you a Dog that prints Woof! I am <name> and a Puppy that extends it. Puppy.bark() is overridden but empty. Fill it so a puppy first does the dog's bark, then prints Yip! Said <name>. A plain Dog must still print only its own line.
The driver creates two puppies and one dog. This is what the finished code prints:
Woof! I am Buddy
Yip! Said Buddy
Woof! I am Rex
Yip! Said Rex
Woof! I am Max
Approach
There are two ways to write a subclass method that "does a bit more":
- Copy the parent's code into the override and add the new line. This works until the parent changes. Then every copy has to be found and updated.
- Call the parent's method with
super, then add the new behavior. The parent's code stays in one place.
The lesson asks for the second way. The order matters: the parent's line comes first, the puppy's own line second, so the output reads "Woof" then "Yip".
The same idea shows up whenever a subclass specializes a parent. The parent owns the general behavior, the subclass adds what is specific to it, and super is the link between them. If the dog's greeting ever changes, every puppy picks up the change without editing Puppy.
Step by step
- Call the parent first. The first line of
Puppy.bark()issuper.bark(). The keywordsuperrefers to the parent class,Dog, but the call still runs on the current object. That is why it prints the puppy's own name and not some other dog's. - Add the puppy's own behavior. After the call, print
Yip! Saidfollowed by the name. Inside a string,$nameinserts the value of thenameproperty.
Nothing else changes. Dog and its bark stay as they are, so Dog("Max").bark() still prints one line.
The solution in Kotlin
open class Dog(val name: String) {
open fun bark() {
println("Woof! I am $name")
}
}
class Puppy(name: String) : Dog(name) {
override fun bark() {
super.bark()
println("Yip! Said $name")
}
}
class Solution {
fun mainFunction() {
val buddy = Puppy("Buddy")
buddy.bark()
val rex = Puppy("Rex")
rex.bark()
val dog = Dog("Max")
dog.bark()
}
}
A few Kotlin details are worth knowing here.
open and override
Kotlin classes and methods are final by default. Dog has to be open before Puppy can extend it, and bark has to be open before Puppy can override it. The override itself needs the override keyword. Leave out open on bark and the compiler reports that bark in Dog is final and cannot be overridden. Leave out open on the class and it says the type is final, so it cannot be extended.
Passing the name up
In class Puppy(name: String) : Dog(name), the parameter name is only a constructor parameter, not a property. It is handed to Dog(name), whose val name stores it. Inside bark, $name reads the property inherited from Dog.
The call follows the object, not the variable
Which bark runs depends on the object that was created. With val d: Dog = Puppy("Sky"), calling d.bark() prints Woof! I am Sky and then Yip! Said Sky, even though the variable's type is Dog. The puppy's override runs, and it calls up to the dog's version through super.
Edge cases and common mistakes
Forgetting super
If the override only prints the Yip line, it replaces the parent's behavior. A Loud("Zed") built that way prints just Yip! Said Zed, and the Woof line is gone. The lesson's tests expect both lines for a puppy.
Wrong order
Putting super.bark() after the println still compiles and still prints both lines, but in the opposite order. For a puppy named Kit the output is Yip! Said Kit and then Woof! I am Kit. The expected output wants the parent's line first.
Calling bark() without super
Writing a plain bark() inside Puppy.bark() does not reach the parent. It calls the same overriding method again, which calls itself again, and so on. The program ends with a StackOverflowError. The word super is what points the call at the parent's version.
Changing Dog
The task is only about Puppy.bark(). Adding the Yip line to Dog would make the plain dog print it as well, and the last line of the expected output would be wrong.
Testing
The driver already covers the three cases that matter: a puppy named Buddy, a second puppy named Rex to show the name comes from the object, and a plain Dog named Max to show the parent is unchanged. One more thing worth trying is a Puppy stored in a Dog variable, as shown above.
Key takeaways
- Overriding replaces the parent's method;
super.method()brings its behavior back in. - Call
superfirst when the subclass should add something after the parent's work. - The parent class needs
open, the method needsopen, and the override needsoverride.
