Is there ever a legitimate reason to call the base class version of a different method?
-
Calling 'super' or 'base' from an overridden version of the same method makes a great deal of sense to me. I have encountered cases where someone has called the base version of a different method, though, and it has seemed illegitimate every time I have encountered it. For example, someone will call something like this: void draw() { if (condition()) super.collectObjects(objects); ... } ...and the overridden collectObjects will be something like this: @Override void collectObjects(List<Object> objects) { if (!condition()) super.collectObjects(); } This was done so that only the initial draw would collect the objects regardless of the condition (skipping the condition by calling the superclass version), and anything recursed would have to check the condition. Calling super.collectObjects from collectObjects, fine, but calling super.collectObjects from draw, to my mind, not fine. There seem much more legitimate means of accomplishing that, though. Has anyone encountered a legitimate use of calling the base class version of a different method? In the absence of that, I would suggest this be considered at least a warning.
-
Answer:
I have done this before under the following circumstance. In a case where I needed to completely change the behavior of a method from what its parent was doing and I still needed access to the behavior of my grandparent class. In this case I wrote a method in the parent class that called the grandparent method. And from the grandchild I called the other parent method. class grandparent { ImportantMethod() { ... } } class parent { @override ImporantMethod() { super.ImportantMethod(); ...//Put in parent functionality } CallParentImportantMethod() { super.ImportantMethod() } } class child { @override ImportantMethod() { ...//Implement functionality that is not compatible with parent but still needs behavior from other classes in the chain higher than parent super.CallParentImportantMethod(); } } And yes I know that this is not the best thing to do but given the constraints of what I was doing it was not possible to refactor to avoid this. As a best practice I would definitely say avoid calling other parent methods.
Joseph Hopper at Quora Visit the source
Other answers
It is bad practice because it makes an assumption about what the Base Class implements and how the method works in the base class and all inheriting classes. Doing this defeats the idea of Polymorphism and information hiding.
Jack Menendez
I have a spectrum of responses: My first thought was that I've never even thought about doing this, so it must be fundamentally wrong ;) My second thought was that I've used super.sameMethod() often enough, so it must either be OK, or there is even a problem with doing that. Perhaps, the distinction is that there is an order of magnitude more cohesion between an overriding method and its parent than between a random subclass method and another superclass method... ...which led me to question this cohesion, which brought me to the position that the very existence of a subclass (by extension, not merely another implementation of the same interface) has a very high coupling to its parent and, in effect, is coupled to every last detail of what its parent is and that any change in the parent is also a change to the subclass, so there isn't that much difference between calling super.sameMethod() than super.otherMethod().
Cornel Masson
upd: misunderstood the question, see comments under this answer.
Vitalie Vrabie
Related Q & A:
- What is the difference between a static method and class method in Python?Best solution by pythoncentral.io
- Is it correct to extend a bean class and write a class which holds logic to populate the bean we extend?Best solution by stackoverflow.com
- Anyone have a website for a LEGITIMATE online job?Best solution by Yahoo! Answers
- How do I transfer ALL of my songs onto a different iTunes on a different computer?Best solution by Yahoo! Answers
- What is a good reason for working in retail?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.