Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Don't use the <code>super</code> keyword to refer to other methods which aren't overridden. It makes it confusing for other developers trying to extend your classes.</p> <p>Let's look at some code which <em>does</em> use the <code>super</code> keyword in this way. Here we have 2 classes: <code>Dog</code> and <code>CleverDog</code>:</p> <pre><code>/* file Dog.java */ public static class Dog extends Animal { private String name; public Dog(String name) { this.name = name; } public String getName() { return name; } } /* file CleverDog.java */ public class CleverDog extends Dog { public CleverDog(String name) { super(name); } public void rollover() { System.out.println(super.getName()+" rolls over!"); } public void speak() { System.out.println(super.getName() + " speaks!"); } } </code></pre> <p>Now, imagine you are a new developer on the project, and you need some specific behavior for a clever dog who is on TV: that dog has to do all its tricks, but should go by its fictitious TV name. To accomplish this, you override the <code>getName(...)</code> method...</p> <pre><code>/* file DogOnTv.java */ public class DogOnTv extends CleverDog { String fictionalName; public DogOnTv(String realName, String fictionalName) { super(realName); fictionalName = fictionalName; } public String getName() { return fictionalName; } } </code></pre> <p>... and fall into a trap set by the original developer and their unusual use of the <code>super</code> keyword!</p> <p>The code above isn't going to work - because in the original <code>CleverDog</code> implementation, <code>getName()</code> is invoked using the <code>super</code> keyword. That means it always invokes <code>Dog.getName()</code> - irrelevant of any overriding. Consequently, when you use your new <code>DogOnTv</code> type...</p> <pre><code> System.out.println("Showcasing the Clever Dog!"); CleverDog showDog = new CleverDog("TugBoat"); showDog.rollover(); showDog.speak(); System.out.println("And now the Dog on TV!"); DogOnTv dogOnTv = new DogOnTv("Pal", "Lassie"); dogOnTv.rollover(); </code></pre> <p>... you get the wrong output:</p> <pre><code>Showcasing the Clever Dog! Tugboat rolls over! Tugboat speaks! And now the Dog on TV! Pal rolls over! Pal speaks! </code></pre> <p>This is not the usual expected behavior when you override a method, so you should avoid creating this kind of confusion using the <code>super</code> keyword where it doesn't belong.</p> <p>If, however, this is actually the behavior you want, use the <code>final</code> keyword instead - to clearly indicate that the method can't be overridden:</p> <pre><code>/* file CleverDog.java */ public class CleverDog extends Dog { public CleverDog(String name) { super(name); } public final String getName() { // final so it can't be overridden return super.getName(); } public void rollover() { System.out.println(this.getName()+" rolls over!"); // no `super` keyword } public void speak() { System.out.println(this.getName() + " speaks!"); // no `super` keyword } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload