Java - extending a class and reusing the methods?

aryaxt picture aryaxt · Jun 24, 2011 · Viewed 27.8k times · Source
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();
   }
}

Answer

Jeanne Boyarsky picture Jeanne Boyarsky · Jun 24, 2011

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.