public class BaseClass {
   public void start() {
      // do something
   }
}
public class ClassA extends BaseClass {
}
ClassA c = new ClassA();
c.start();
In the following code I want to use the start() method as it was defined in the super class, I have seen in a lot of other developer's codes that they override the method in the super class and then they call the super. is there a reason for that?
public class ClassA extends BaseClass {
   @Override
   public void start() {
      super.start();
   }
}
                Clarity? Some developers feel it is clearer to show the method in the subclass. I disagree. It's redundant info.
At least since Java 5, you could add an @Override so the compiler will tell you if the signature changes/disappears.
Except for constructors. For constructors, you really do have to create your own with the same signature and delegate upwards. In this case, omitting isn't equivalent though.
Overriding a method, doing something special, then calling super.method() is called decorating a method - you're adding to the behaviour. Read more here: Decorator Pattern.
Overriding it without calling super's method is simply overriding a method - changing the behaviour.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With