开发者

Best way to invoke method inhereted from Base class : use this , super or call implicitly

I think it's not very useful question, but i think someone can say smth critical.

public class Base{
    public void boo(){}
}

public class Derivative extends Base{

1开发者_StackOverflow中文版:public void useBoo(){
    boo();
}

2:public void useBoo(){
    this.boo();
}

3:public void useBoo(){
    super.boo();
}
}

[EDIT]

The reason why I asked this question : to avoid recompilation or name confusion in future. And make code more readability and flexable to future changes.


The best one to use is the one you feel is clearest.

BTW: The first example will behave different to the third example, if you add a boo method to Derived.


If you want to be sure that the method invoked is the super class method, definitely use super:

super.boo();

If you want to use a method defined in the current class and if this is not present use the super method use:

this.boo();
//or
boo();

Personally in the second case, if the class is part of a hierarchy, I prefer to explicitly use this.boo() because it's more clear reading the code, to understand where the method is supposed to be. But it's my opinion.


I would only use 1 and 2 (in my case I prefer 2, I like to be explicit about my intentions)

public void useBoo(){
    this.boo();
}

I wouldn't use 3 because it means something else semantically, remember that Derivative can also be extended, and boo() may be overridden further down the class hierarchy, and unless you specifically want to call Base.boo() you shouldn't use the super keyword.


Use one of the first two, unless you explicitly want to limit the call to the superclass implementation.

When you override a superclass method, and want to include the behavior of the superclass method, use the third.

Elsewhere, if you use the third, and later you or someone else overrides the superclass implementation of boo() -- in this class or a subclass -- then useBoo() will not use the override. This is usually undesirable except in the override of boo() itself.

Examples:

/** Extends boo method. */
public void boo(){
    super.boo();   // Good: you explicitly want to call the superclass method, not call this method again.
}

public void doSomethingThatInvolvesBoo() {
    boo();        // Good -- will call this object's implementation,
                  // even if this is a subclass, even if there are code changes later.
    this.boo();   // Good (and equivalent to simply calling boo()).
    super.boo():  // Usually bad.
}


Is there anything stopping you from just calling boo()? Or have you overridden it in your own code? 1 and 2 do the same thing, 3 calls the method of Base.

edit: as Simeon said, using super would make it very clear you are calling the method from the Base class.


Having readability in mind super is probably the best because it says here I'm invoking a method from the super class.

While with this you could wonder 'where the hell is this method .. oh ok its in the super class'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜