Use of 'super' keyword when accessing non-overridden superclass methods

后端 未结 8 2030
梦如初夏
梦如初夏 2021-01-02 12:38

I\'m trying to get the hang of inheritance in Java and have learnt that when overriding methods (and hiding fields) in sub classes, they can still be accessed from the super

相关标签:
8条回答
  • If you use super, you are explicitly telling to use super class method (irrespective of sub class has overridden method or not), otherwise first jvm checks for the method in subclass (overridden method if any), if not available uses super class method.

    0 讨论(0)
  • 2021-01-02 12:46

    overriding means redefining a method from the superclass inside a subclass with identical method signature. In your case, the getTyreCost() method has not been overridden, you have not redefined the method in your subclass, so no need to use super.getTyreCost(), only getTyreCost() will do(just like super.getTyreCost() will do the same way). super keyword is used when a method has been overridden, and you want a method call from within your subclass to be implemented in the superclass.

    0 讨论(0)
  • 2021-01-02 12:47

    This would be dependent on how you plan to use the code. If you specify super.getTyreCost() and then later override that method. You will still be calling the method on the superclass, not the overridden version.

    In my opinion, calling super is likely to lead to more confusion later on, so is probably best specified only if you have an explicit need to do so. However, for the case you have presented here - there will be no difference in behavior.

    0 讨论(0)
  • 2021-01-02 12:48

    Don't use the super keyword to refer to other methods which aren't overridden. It makes it confusing for other developers trying to extend your classes.

    Let's look at some code which does use the super keyword in this way. Here we have 2 classes: Dog and CleverDog:

    /* 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!");
        }
    
    }
    

    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 getName(...) method...

    /* 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;
        }
    
    }
    

    ... and fall into a trap set by the original developer and their unusual use of the super keyword!

    The code above isn't going to work - because in the original CleverDog implementation, getName() is invoked using the super keyword. That means it always invokes Dog.getName() - irrelevant of any overriding. Consequently, when you use your new DogOnTv type...

        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();
    

    ... you get the wrong output:

    Showcasing the Clever Dog!
    Tugboat rolls over!
    Tugboat speaks!
    
    And now the Dog on TV!
    Pal rolls over!
    Pal speaks!
    

    This is not the usual expected behavior when you override a method, so you should avoid creating this kind of confusion using the super keyword where it doesn't belong.

    If, however, this is actually the behavior you want, use the final keyword instead - to clearly indicate that the method can't be overridden:

    /* 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
        }
    
    }
    
    0 讨论(0)
  • 2021-01-02 12:56

    Technically, the one that's invoked in this case is the inherited version, for which the implementation is actually provided by the parent class.

    There can be scenarios where you must use the super keyword. e.g. if Car had overridden that method, to provide a different implementation of itself, but you needed to invoke the implementation provided by the parent class then you would have use the super keyword. In that case, you could not afford to omit the super keyword because if you did then you would be invoking the implementation provided by the child class itself.

    0 讨论(0)
  • 2021-01-02 13:02

    It depends on your needs and your desires. Using super forces the compile/application to ignore any potential methods in your current class. If you want to communicate that you only want to use the parent's method, then using super is appropriate. It will also prevent future modifications to your class to accidentally override the parent method thereby ruining your expected logic.

    However, these cases are fairly rare. Using super everywhere within your class will lead to very confusing & cluttered code. In most general cases, just calling the method within your own class and allowing the compiler/jvm to determine which method (super or local) needs to be called is more appropriate. It also allows you to override/modify/manipulate the super's returning values cleanly.

    0 讨论(0)
提交回复
热议问题