Start learning free

LESSON 38 OF 69 · OBJECT-ORIENTED PROGRAMMING

super in Java: Calling the Parent's Method

Practice Read the full explanation

TL ; DR:

Inside an overriding 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 extends Dog {
    public Puppy(String name) { super(name); }
    @Override
    public void bark() {
        super.bark();   // do everything the parent did
        System.out.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() (note the @Override), 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 System.out.println("Yip! Said " + name);.

Notes:

  • super(name) in the constructor (already written) passes the name up to Dog's constructor; super.bark() does the same for a method.
  • name is a public field of Dog, so Puppy can read it directly.

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

Puppy buddy = new Puppy("Buddy");
buddy.bark();
Puppy rex = new Puppy("Rex");
rex.bark();
Dog dog = new 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

    JAVA · OBJECT-ORIENTED PROGRAMMING

    Base Keyword in Java, explained

    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

    1. Call the parent first. Inside Puppy.bark(), write super.bark();. This runs Dog's body for the same object, so the Woof! line, with the puppy's own name, is printed.
    2. Add the puppy's own line. Print "Yip! Said " followed by name. The field name is declared public in Dog and inherited, so Puppy reads 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) and super.bark() are different things. The first is a constructor call. It may only appear as the first statement of a constructor and passes name up so Dog'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 name field, so Rex prints Rex in both. Nothing is hard-coded.
    • @Override is 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 as Bark().
    • super reaches one level up. It names the direct parent class. If Dog had its own parent that also defined bark, super.bark() in Puppy would run Dog's version, which could in turn call its parent's.
    • The object decides which bark runs. With Dog d = new Puppy("Zed"); d.bark(); the variable's type is Dog, yet the output is Woof! I am Zed followed by Yip! Said Zed, because the object is a Puppy.

    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 only Yip! Said Pip.
    • Calling bark() without super.. Inside Puppy.bark(), a bare bark() calls the same method again, which calls itself again, and the program ends with a StackOverflowError. The word super is what sends the call up to Dog.
    • Wrong order. Printing the puppy's line first and calling super.bark() second is valid Java, but it prints Yip! Said Pip before Woof! I am Pip, which doesn't match the expected output.
    • Changing Dog. The task only touches Puppy.bark(). The plain Dog named Max must 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 Puppy whose bark() prints its own line first and calls super.bark() second prints Yip! Said Pip, then Woof! I am Pip.
    • A Puppy whose bark() only prints its own line prints just Yip! Said Pip.
    • Dog d = new Puppy("Zed"); d.bark(); prints Woof! I am Zed and then Yip! Said Zed.

    Key takeaways

    • Overriding replaces a method; super.method() inside the override brings the parent's behaviour back in.
    • Put the super call 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, and super.name(...) in a method to reuse the parent's method.
    Keep going with Java1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free