Start learning free

LESSON 395 OF 404 · OBJECT-ORIENTED PROGRAMMING

Calling the Embedded Parent in Go in Go

Practice

TL ; DR:

Go has no classes or super. Puppy embeds a Dog, and when Puppy defines its own bark it hides the embedded one. The embedded Dog is still there as this.Dog, so this.Dog.bark() runs the parent's version. This lets you extend the parent's behavior instead of replacing it.


func (this Puppy) bark() {
    this.Dog.bark()   // do everything the parent did
    fmt.Printf("Yip! Said %s\n", this.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

Dog (with its bark method), Puppy (which embeds Dog) and the constructor NewPuppy are written. You write the bark method of Puppy: func (this Puppy) bark(). Inside it:

  1. call the embedded parent's version with this.Dog.bark(), so Woof! I am <name> still prints;
  2. then fmt.Printf("Yip! Said %s\n", this.name).

Notes:

  • Puppy is declared as struct { Dog } (embedding). NewPuppy(name) builds it and is already written.
  • this.name works because the embedded Dog's fields are promoted to Puppy.

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

buddy := NewPuppy("Buddy")
buddy.bark()
rex := NewPuppy("Rex")
rex.bark()
dog := Dog{name: "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 3
    Ctrl + Enter to check

    Base Keyword in Go

    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free