By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,783 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,783 IT Pros & Developers. It's quick & easy.

Hiding inherited method

P: n/a
I was trying to break some polymorphism, expecting it not to work, but I'm a
curious sort.

I was seeing what happens when a derived class tries to hide an inherited
method with a private new method, expecting an error or warning; I got
neither with the result that the inherited method does _not_ get hidden (i.e.
not possibe to break polymorphism/inheritance this way, yay!)

My question then is, why is there no warning that the code may not work the
way the writer expects? Is it simply because the _new_ keyword turns it off?
Without the _new_ keyword, the warning about hiding fires, but this is a case
where no hiding will happen anyway, because the private method _does_not_
hide the inherited method.
Mar 30 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
PIEBALD <PI*****@discussions.microsoft.com> wrote:
I was trying to break some polymorphism, expecting it not to work, but I'm a
curious sort.

I was seeing what happens when a derived class tries to hide an inherited
method with a private new method, expecting an error or warning; I got
neither with the result that the inherited method does _not_ get hidden (i.e.
not possibe to break polymorphism/inheritance this way, yay!)

My question then is, why is there no warning that the code may not work the
way the writer expects? Is it simply because the _new_ keyword turns it off?
Without the _new_ keyword, the warning about hiding fires, but this is a case
where no hiding will happen anyway, because the private method _does_not_
hide the inherited method.


Well, it hides it within that class - it just doesn't hide it for other
users of the class.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 30 '06 #2

P: n/a
And, by casting the derived object to the base class, I can even call
the "hidden" method!
Bob
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
PIEBALD <PI*****@discussions.microsoft.com> wrote:
I was trying to break some polymorphism, expecting it not to work, but
I'm a
curious sort.

I was seeing what happens when a derived class tries to hide an inherited
method with a private new method, expecting an error or warning; I got
neither with the result that the inherited method does _not_ get hidden
(i.e.
not possibe to break polymorphism/inheritance this way, yay!)

My question then is, why is there no warning that the code may not work
the
way the writer expects? Is it simply because the _new_ keyword turns it
off?
Without the _new_ keyword, the warning about hiding fires, but this is a
case
where no hiding will happen anyway, because the private method _does_not_
hide the inherited method.


Well, it hides it within that class - it just doesn't hide it for other
users of the class.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 30 '06 #3

P: n/a
Bob Milton <Do********@newsgroup.nospam> wrote:
And, by casting the derived object to the base class, I can even call
the "hidden" method!


Absolutely - but that's always true with "hiding", as anything else
would break Liskov's Substitution Principle.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 31 '06 #4

P: n/a
True. But it still amazes me how many people want to be able to
eliminate a public function in the base class (which in essence you can't
really do).
Bob
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bob Milton <Do********@newsgroup.nospam> wrote:
And, by casting the derived object to the base class, I can even call
the "hidden" method!


Absolutely - but that's always true with "hiding", as anything else
would break Liskov's Substitution Principle.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 31 '06 #5

P: n/a
Bob Milton <Do********@newsgroup.nospam> wrote:
True. But it still amazes me how many people want to be able to
eliminate a public function in the base class (which in essence you can't
really do).


Absolutely. Like regular expressions, I regard inheritance as a
powerful tool which is unfortunately overused. See

http://msmvps.com/jon.skeet/archive/...itancetax.aspx

for a more detailed rant :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 31 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.