Re: Suggestion:child class overrides parent's method but does not call super()

View: New views
3 Messages — Rating Filter:   Alert me  

Parent Message unknown Re: Suggestion:child class overrides parent's method but does not call super()

by Andreas Probst :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It's perfectly fine not to call super from a sub-class. And the
longer I think about it, I believe it's rather uncommon to do so.

Exception: In constructors you usually call super().

On 5 Jun 2008 at 0:34, Imran Fanaswala wrote:

> Hello there,
>
> I wonder why such an obvious gotcha has not been considered and
> incorporated into Findbugs... Here's a simple example:
> http://pastebin.com/mc404130
>
> Note, this is true even if the parent class is abstract
>
> Regards,
> Imran.
>


_______________________________________________
Findbugs-discuss mailing list
Findbugs-discuss@...
https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss

Re: Suggestion:child class overrides parent's method but does not call super()

by Imran Fanaswala :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes, you are right that calling super() is uncommon, but that does not justify it's correctness.
Let me establish some points here.

1)
Calling super() in an overriden method should cause no harmful effects whatsoever. On the other hand, not calling super() can possibly result in some defects. One example is in the case of methods responsible for freeing resources - a failure to call super.cleanUp() can lead to leaks. Maybe. Maybe not. Why take the chance?

2)
If calling super() in your overriden method alters the semantics of your code, this is an indicator that you've gotten your inheritance wrong. Dodgy design perhaps. Hence another reason why calling super() is beneficial. You hit 2 birds with the same stone.

3)
Ultimately, calling super() is an aspect of writing "defensive code". FindBugs is a software that not only helps find bugs, but prevents them, enforces "defensive programming", highlights subtleties, prevent dodgy coding and "gotchas" etc. Therefore I feel this should be implemented.

A penny for your thoughts, folks?


On Thu, Jun 5, 2008 at 1:46 AM, Andreas Probst <andpro77@...> wrote:
It's perfectly fine not to call super from a sub-class. And the
longer I think about it, I believe it's rather uncommon to do so.

Exception: In constructors you usually call super().

On 5 Jun 2008 at 0:34, Imran Fanaswala wrote:

> Hello there,
>
> I wonder why such an obvious gotcha has not been considered and
> incorporated into Findbugs... Here's a simple example:
> http://pastebin.com/mc404130
>
> Note, this is true even if the parent class is abstract
>
> Regards,
> Imran.
>


_______________________________________________
Findbugs-discuss mailing list
Findbugs-discuss@...
https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss


_______________________________________________
Findbugs-discuss mailing list
Findbugs-discuss@...
https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss

Re: Suggestion:child class overrides parent's method but does not call super()

by Nils Kilden-Pedersen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Jun 4, 2008 at 6:35 PM, Imran Fanaswala <imran.fanaswala@...> wrote:
Calling super() in an overriden method should cause no harmful effects whatsoever.

That is not correct. An example would be ByteBuffer#put(byte[], int, int). It's being overridden in HeapByteBuffer for performance reasons, and calling super would write data twice. Seems quite harmful.

Nils

_______________________________________________
Findbugs-discuss mailing list
Findbugs-discuss@...
https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss
LightInTheBox - Buy quality products at wholesale price