473,769 Members | 6,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2118
PIEBALD <PI*****@discus sions.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
PIEBALD <PI*****@discus sions.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.co m>
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********@new sgroup.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Bob Milton <Do********@new sgroup.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.co m>
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********@new sgroup.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.co m>
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
389
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 similar to what I'm thinking about. Lets suppose I make a class called RoleCollection.
7
8668
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
3753
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 co-ordinate) derived from Point3
6
6663
by: Microsoft | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
4
1636
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 handful of inherited classes. The static method calls the ctor of the appropriate inherited class and returns it as type IBaseClass. There are no new methods or properties in the inherited classes and public clients never need to know which...
4
14867
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' keyword in java. Thanks.
7
5177
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 inherited member by defining a new member with the same signature. This might be done to make a previously public member private or to define new behavior for an inherited method that is marked as final. " However, this does not hide the...
7
1189
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 application? For example, if Class "A" has a "StateChanged" event that I don't want the application to see, is there a way to hide exposure to that event in Class "B"? Thanks for any help.
14
2428
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 project/assembly). I tried with attributes: <Browsable(False), _ EditorBrowsable(EditorBrowsableState.Never), _ RefreshProperties(RefreshProperties.Repaint), _
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.