Reputation: 3955
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 class by using the 'super' keyword.
What I want to know is, should the 'super' keyword be used for non-overridden methods?
Is there any difference (for non-overridden methods / non-hidden fields)?
I've put together 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, given that getTyreCost()
hasn't been overridden, should getTotalTyreReplacementCost()
use getTyreCost()
, or super.getTyreCost()
?
I'm wondering whether super should be used in all instances where fields or methods of the superclass are accessed (to show in the code that you are accessing the superclass), or only in the overridden/hidden ones (so they stand out).
Upvotes: 8
Views: 7962
Reputation: 28753
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
}
}
Upvotes: 13
Reputation: 51030
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.
Upvotes: 0
Reputation: 11025
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.
Upvotes: 0
Reputation: 1144
It is advised that further changes to the inherited class will not necessitate addition of the super qualifier and also prevent errors if missed.
Upvotes: -1
Reputation: 24411
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.
Upvotes: 2
Reputation: 7848
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.
Upvotes: 2
Reputation: 1559
You are doing the right way by not using the super
keyword for accessing getTyreCost
.
But you should set your members private and only use the getter method.
Using super
keyword should be reserved for constructors and overridden methods which need to explicitly call the parent method.
Upvotes: 3
Reputation: 66637
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.
Upvotes: 1