[ACCEPTED]-Java - extending a class and reusing the methods?-inheritance

Accepted answer
Score: 11

Clarity? Some developers feel it is clearer 8 to show the method in the subclass. I disagree. It's 7 redundant info.

At least since Java 5, you 6 could add an @Override so the compiler will 5 tell you if the signature changes/disappears.

Except 4 for constructors. For constructors, you 3 really do have to create your own with the 2 same signature and delegate upwards. In 1 this case, omitting isn't equivalent though.

Score: 10

Overriding a method, doing something special, then 4 calling super.method() is called decorating a method - you're adding to 3 the behaviour. Read more here: Decorator Pattern.

Overriding 2 it without calling super's method is simply 1 overriding a method - changing the behaviour.

Score: 6
public class ClassA extends BaseClass {
  @Override
  public void start() {
     super.start();
  }
}

does exactly the same thing as not overriding 9 at all like this

public class ClassA extends BaseClass {}

So unless you have some 8 extra functionality to add (in which case 7 you call super class's method and then add 6 your extra logic) or to do something different(you 5 don't call super class's method and just 4 define some different logic), it's best 3 you don't override super class's method 2 (and call super class's method) because 1 it's pointless.

Score: 1

I sometimes do this (temporarily, during 2 development) when I want to set a break-point 1 there.

Score: 0

The main reason behind the concept of inheritance 8 is generalization of the functions or operations 7 that are common among sub classes. It makes 6 sense to override only when we need to customize 5 the operation for the particular sub-class.

  • But one place where we do need to make an override is, say you have overloaded your super class default constructor, then in sub-class you need to mention the invocation of the overloaded super-class constructor as the first line in your sub-class constructor(ie Super-class object has to be created before sub-class creation). For example

class 4 BaseClass{

Baseclass(arg1){

}

}

class A extends 3 BaseClass{

A{ super(arg1); }

}

In 2 other places it would only add a redundancy 1 of the code, which is not at all necessary

More Related questions