473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Hiding inherited method

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
5 2097
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: thechaosengine | last post by:
Hi all, Is there a way to hide a member in a subclass that has been inherited from a base class? Lets leave aside any issues regarding whether its a good idea for a moment. Here's an example...
7
by: Tron Thomas | last post by:
Under the right compiler the following code: class Base { public: virtual void Method(int){} }; class Derived: public Base {
4
by: A Ratcliffe | last post by:
Back again... I have some classes that inherit from each other, as an example... Point (abstract base class) Point3 (for 3D points), derived from Point Point4 (adds a homogenous...
6
by: Microsoft | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
4
by: Dan | last post by:
I have a need to make a set of classes that all share the same public methods, some implementation and some data. So, I made an abstract base (BaseClass) with an interface (IBaseClass) and a...
4
by: yaron | last post by:
Hi, how can i prevent from a derive class to override/hide a base class method ? because in c# there is the new keyword on a method. i am looking for the same functionality as the 'final'...
7
by: Dennis | last post by:
I have a class named myclass that inheirits from "baseclass". There is a property of "baseclass" that I don't want exposed in the IDE. The MSDN documentation says" "A derived type can hide an...
7
by: OpticTygre | last post by:
If I have a Class "B" that Inherits Class "A", and my application uses class "B", is there a way that I can keep from exposing properties, subs, and events in the Class "A" base class from the...
14
by: Dom | last post by:
Hi all I'm developing a control, and I need to hide some properties to the user. For example, suppose I need Text property to be completely inacessible (from a Form/Code that is into another...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.