E.g. :
public class ClassA extends ClassB {
public void run() {
super.execute();
...................
Method execute exists only in ClassB.
Does it make sense to use:
super.execute();
may be enough:
execute();
?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Reasons:
Predictability (safety?) – if one day somebody implements
ClassA.execute(), the code will work differently. This is also the case even is somebody subclassesClassAand overridesexecute(), leavingClassAintact.Performance – super call may be faster than virtual.
superis implemented usinginvokespecial(single dispatch, just likeprivatemethod), while ordinary call to non-private method uses double dispatch (virtual call). This is a weak advantage in modern JVMs.Bottom line: if
ClassB.execute()isfinal, usingsuperhas no sense.