Start learning free

LESSON 38 OF 70 · OBJECT-ORIENTED PROGRAMMING

Base Keyword in Kotlin: Calling the Parent with super

Practice Read the full explanation

TL ; DR:

Inside an override method, super.bark() calls the parent's version of bark on the same object. This lets you extend the parent's behavior instead of replacing it.


class Puppy(name: String) : Dog(name) {
    override fun bark() {
        super.bark()   // do everything the parent did
        println("Yip! Said $name")
    }
}

Why it matters

Overriding a method replaces the parent's behavior. Often you want to extend it instead: keep what the parent did and add something more. A typical example is a save method: the parent writes the row to the database, and a subclass runs the parent's save first and then writes a log line.

Your task

The Puppy class already overrides bark(), but the body is empty. Inside Puppy.bark():

  1. call the parent's version with super.bark(), so Woof! I am <name> still prints;
  2. then println("Yip! Said $name").

Notes:

  • In Kotlin a class can be extended only if it is open, and a method overridden only if it is open. Dog and Dog.bark are already marked that way.
  • Puppy(name: String) : Dog(name) passes the name to Dog's constructor; $name inside a string inserts the name property.

The driver (already written, it runs when you press Run):

val buddy = Puppy("Buddy")
buddy.bark()
val rex = Puppy("Rex")
rex.bark()
val dog = Dog("Max")
dog.bark()
  • buddy is a Puppy: its bark prints the parent's line, then its own.
  • rex is another Puppy: the names come from the object, nothing is hard-coded.
  • dog is a plain Dog: its bark is unchanged and prints only the parent's line, with no Yip!.

Expected output:

Woof! I am Buddy
Yip! Said Buddy
Woof! I am Rex
Yip! Said Rex
Woof! I am Max
STEP 1 OF 2
    Ctrl + Enter to check

    KOTLIN · OBJECT-ORIENTED PROGRAMMING

    Base Keyword in Kotlin, explained

    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

    1. Call the parent first. The first line of Puppy.bark() is super.bark(). The keyword super refers 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.
    2. Add the puppy's own behavior. After the call, print Yip! Said followed by the name. Inside a string, $name inserts the value of the name property.

    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 super first when the subclass should add something after the parent's work.
    • The parent class needs open, the method needs open, and the override needs override.
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free