Java – use the ‘super’ keyword when accessing non overridden superclass methods

I've tried to get inheritance rights in Java and have learned that when overriding methods (and hidden fields) in subclasses, they can still be accessed from superclasses using the "super" keyword

What I want to know is, should the 'super' keyword be used for non overridden methods?

Is there a difference (for non overriding methods / non hidden fields)?

I give an example below

public class Vehicle {
    private int tyreCost;

    public Vehicle(int tyreCost) {
         this.tyreCost = tyreCost;
    }

    public int getTyreCost() {
        return tyreCost;
    }        
}

and

public class Car extends Vehicle {
    private int wheelCount;

    public Vehicle(int tyreCost,int wheelCount) {
        super(tyreCost);
        this.wheelCount = wheelCount;
    }   

    public int getTotalTyreReplacementCost() {
        return getTyreCost() * wheelCount;
    }   
}

Specifically, assuming that gettyrecost () is not overwritten, should gettotaltyrereplacementcast () use gettyrecost () or super getTyreCost()?

I want to know whether super should be used in all instances of fields or methods that access the superclass (show in the code that you are accessing the superclass), or only in those instances that are overridden / hidden (so they stand out)

Solution

Do not use the super keyword to reference other methods that are not overwritten This confuses other developers trying to extend your class

Let's look at some code that uses the super keyword in this way There are two 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 that you are the new developer of this project. You need some specific behavior of smart dogs on TV: the dog must do all the skills, but it should pass its fictional TV name To complete this operation, 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 the trap set by the original developers and their unusual use of super keywords!

The above code doesn't work - because in the original cleverdog implementation, getname () was called with the super keyword This means that it always calls dog Getname () – independent of any rewriting So when you use the 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 confusion by using superkeywords that do not belong to it

However, if this is actually the behavior you want, use the final keyword – clearly indicating that the method cannot 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
    }

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>