Re: explicitly invoking a superclass method
From: John C. Bollinger (jobollin_at_indiana.edu)
Date: 06/10/04
- Next message: Jean Charbonneau: "Re: What is the best way to search for a set of values within a multi-dimenstional array?"
- Previous message: Chris Roe: "Performance problems on Intel but not AMD... strange"
- In reply to: Andy Fish: "explicitly invoking a superclass method"
- Next in thread: Roedy Green: "Re: explicitly invoking a superclass method"
- Reply: Roedy Green: "Re: explicitly invoking a superclass method"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 10 Jun 2004 12:10:03 -0500
Andy Fish wrote:
> When maintaining an old piece of code written by someone I never met (as
> usual), I came across this:
>
> void foo()
> {
> ...
> super.bar();
> ...
> }
>
> As it happens, the class in question doesn't override bar() and nor do any
> of it's subclasses, so I guess it's safe to just replace it by a call to
> bar() - I'm just a bit concerned that maybe the author was trying to get at
> something subtle which I missed
I think it more likely that the original author did not understand
inheritance. Perhaps he was also leery of polymorphism. Does the code
exhibit other signs of inexpert language usage?
> It seems to me that this defeats the object of the whole
> inheritance/polymorphism thing. I would have thought that the only
> legitimate use of super is to inherit the behaviour you are overriding, not
> invoke a completely different method.
I don't know about legitimacy, but it sure obscures the programmer's
intent. If the class depends on the particular implementation provided
by its superclass, then it should override that method with a _final_
method that just invokes the superclass' version, and then other methods
should invoke that method by its unqualified name. That not only makes
the intent clear, but it also prevents mysterious behavior when a
subclass is written that tries to override bar() but not foo().
I'd recommend that you switch to this idiom instead of just dropping the
"super" -- you are certain to get the original behavior, and you have a
chance of discovering some subtle bugs. An alternative that achieves
the same end is to make the superclass' bar() final itself, and then
(again) have subclasses invoke it by its unqualified name, but that has
a broader scope than the one class where you noticed the issue.
> Are there any legitimate circumstances for using this construct? If not, why
> does Java allow it?
Again with the "legitimate". I can't work with that; it's too open to
interpretation. I will half-seriously suggest that the construct is
allowed so as to permit poor code to be written. ;-) More likely, is
that super has to be allowed in some places, and it would complicate
parsing for very little gain to disallow it. The semantics are well
defined and consistent. I don't think it is the job of a language
designer to anticipate and attempt to prevent all language uses that
fall into the "not a good idea" category.
John Bollinger
jobollin@indiana.edu
- Next message: Jean Charbonneau: "Re: What is the best way to search for a set of values within a multi-dimenstional array?"
- Previous message: Chris Roe: "Performance problems on Intel but not AMD... strange"
- In reply to: Andy Fish: "explicitly invoking a superclass method"
- Next in thread: Roedy Green: "Re: explicitly invoking a superclass method"
- Reply: Roedy Green: "Re: explicitly invoking a superclass method"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]