473,564 Members | 2,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Hiding" an inherited property?

Is there a way to make a property of an inherited class invisible to the
programmer? I know that using the keyword "new" allows you to create
another property in the place of the existing one, but how can I make the
property hidden and not available at all?
Thanks,
Jacob
Nov 16 '05 #1
10 12366
Jacob,

I believe you're trying to re-write the fundamentals of inheritance by doing
this.

The official way is to raise an NotImplemented exception within your
handler.

Imagine the following scenario:

class MyBase
{
public virtual void DoSomething()
{
}
}

class MySubclass
{
public overrides void DoSomething()
{
throw new NotImplementExc eption();
}
}

even if you could make the MySubclass::DoS omething invisible it would still
be visible through it's super class

e.g.

MyBase mb = new MySubclass();
MyBase.DoSometh ing();

This is why you need to raise the NotImplementExc eption to prevent calling
this no matter where in the class hierachy that function is called from.

hope that clears it up,

cheers,

g

PS I've wanted to do the same sometimes but you've no choice but to either
subclass a class further up in the food chain or possibly use encapsulation
rather than inheritance to provide the functionality you require.

"Jacob" <ja**********@R EMOVETHIShotmai l.com> wrote in message
news:7_Uec.6111 $ab3.1674@fed1r ead02...
Is there a way to make a property of an inherited class invisible to the
programmer? I know that using the keyword "new" allows you to create
another property in the place of the existing one, but how can I make the
property hidden and not available at all?
Thanks,
Jacob

Nov 16 '05 #2
Ya, I've never found a way either, I was just hoping someone else knew one.
Using the NotImplemented exception is a good practice, thanks for the tip.
I could swear that Microsoft has done it on some of their classes though. I
can't think of a particular example right now, but I'm certain that I've
seen it somewhere.

Thanks,
Jacob
"Gary Hunt" <be*******@code quest.co.uk> wrote in message
news:ur******** *****@tk2msftng p13.phx.gbl...
Jacob,

I believe you're trying to re-write the fundamentals of inheritance by doing this.

The official way is to raise an NotImplemented exception within your
handler.

Imagine the following scenario:

class MyBase
{
public virtual void DoSomething()
{
}
}

class MySubclass
{
public overrides void DoSomething()
{
throw new NotImplementExc eption();
}
}

even if you could make the MySubclass::DoS omething invisible it would still be visible through it's super class

e.g.

MyBase mb = new MySubclass();
MyBase.DoSometh ing();

This is why you need to raise the NotImplementExc eption to prevent calling
this no matter where in the class hierachy that function is called from.

hope that clears it up,

cheers,

g

PS I've wanted to do the same sometimes but you've no choice but to either
subclass a class further up in the food chain or possibly use encapsulation rather than inheritance to provide the functionality you require.

"Jacob" <ja**********@R EMOVETHIShotmai l.com> wrote in message
news:7_Uec.6111 $ab3.1674@fed1r ead02...
Is there a way to make a property of an inherited class invisible to the
programmer? I know that using the keyword "new" allows you to create
another property in the place of the existing one, but how can I make the property hidden and not available at all?
Thanks,
Jacob


Nov 16 '05 #3
I think you can also write something like

public MyClass
{
public virtual string MyProp
{
get{...}
set{...}
}
}

public MyDerivedClass : MyClass
{
private new string MyProp
{
get{ return String.Empty; }
set{ /* do nothing */ }
}
}

HTH

--
mauro servienti

..

"Jacob" <ja**********@R EMOVETHIShotmai l.com> wrote in message
news:7_Uec.6111 $ab3.1674@fed1r ead02...
Is there a way to make a property of an inherited class invisible to the
programmer? I know that using the keyword "new" allows you to create
another property in the place of the existing one, but how can I make the
property hidden and not available at all?
Thanks,
Jacob

Nov 16 '05 #4
"Jacob" <ja**********@R EMOVETHIShotmai l.com> wrote:
Is there a way to make a property of an inherited
class invisible to the programmer? I know that
using the keyword "new" allows you to create
another property in the place of the existing one, but
how can I make the property hidden and not available
at all?


I don't think you can, and it would be a very bad thing to do from an
object-oriented standpoint.

For it to be possible to treat any inherited class instance as though it
were a base class instance, the inherited class must have all of the members
of the base class.

P.
Nov 16 '05 #5
Jacob <ja**********@R EMOVETHIShotmai l.com> wrote:
Is there a way to make a property of an inherited class invisible to the
programmer?


No, fortunately. That would break Liskov's Substitutabilit y Principle -
basically, whatever you can do with a base class, you should be able to
do with a derived class.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Mauro Servienti <ma************ @online.nospam> wrote:
I think you can also write something like

public MyClass
{
public virtual string MyProp
{
get{...}
set{...}
}
}

public MyDerivedClass : MyClass
{
private new string MyProp
{
get{ return String.Empty; }
set{ /* do nothing */ }
}
}


That doesn't do anything though, as far as other classes are concerned
- the fact that it's private means that it's basically irrelevant to
anything beyond the class itself (and any nested types). You could use
a *public* new property to hide the old one, as Jacob had already said,
but that's not what he was after.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
I agree that it's important law of object oriented design that must be
adheard to. If the rules could be bent here or there, then the design of
the whole system would fall appart.

It's just that sometimes, properties no longer seem applicable in some
cases. The next best thing is if the class is a control and can be editted
by the designer, you can at least set an attribute to make it invisible to
the designer.

Jacob

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** @msnews.microso ft.com...
Jacob <ja**********@R EMOVETHIShotmai l.com> wrote:
Is there a way to make a property of an inherited class invisible to the
programmer?


No, fortunately. That would break Liskov's Substitutabilit y Principle -
basically, whatever you can do with a base class, you should be able to
do with a derived class.

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

Nov 16 '05 #8
Jacob <ja**********@R EMOVETHIShotmai l.com> wrote:
I agree that it's important law of object oriented design that must be
adheard to. If the rules could be bent here or there, then the design of
the whole system would fall appart.

It's just that sometimes, properties no longer seem applicable in some
cases. The next best thing is if the class is a control and can be editted
by the designer, you can at least set an attribute to make it invisible to
the designer.


Ah - you *can* do that, I believe, although I can't remember what the
attribute is. Presumably you can then override the existing property
(and maybe throws an exception) and apply the attribute to the
override.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
BrowsableAttrib ute.

-vJ

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Jacob <ja**********@R EMOVETHIShotmai l.com> wrote:
I agree that it's important law of object oriented design that must be
adheard to. If the rules could be bent here or there, then the design of the whole system would fall appart.

It's just that sometimes, properties no longer seem applicable in some
cases. The next best thing is if the class is a control and can be editted by the designer, you can at least set an attribute to make it invisible to the designer.


Ah - you *can* do that, I believe, although I can't remember what the
attribute is. Presumably you can then override the existing property
(and maybe throws an exception) and apply the attribute to the
override.

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

Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2054
by: Picho | last post by:
Hi all, Lets say I have a "secret" I wish to "hide", lets say a database password. For the more detailed problem, a web application/service that uses a connection string. all the solutions I came up with (embedding in code, encrypting-decrypting) involve embedding the/another secret in the code. since my problem cannot request a user...
5
2173
by: Amir S. | last post by:
Hi, I'm a newbie to C++ (2 weeks into the course). We were given this assignment to write some code that reads a set of integers (grades) from a file (filename passed by console), outputs them in reverse order, calculates and prints their average. => We have been told that we need to make sure that our code meets "information hiding"...
10
738
by: Jacob | last post by:
Is there a way to make a property of an inherited class invisible to the programmer? I know that using the keyword "new" allows you to create another property in the place of the existing one, but how can I make the property hidden and not available at all? Thanks, Jacob
10
31680
by: FX | last post by:
I wanna publish a script on my site which allows me to hide image source. i have rough idea abt it. i`ll point src to some php page like: <img src="image.php"> & in tht php wat exactly shud be done so tht user doesnt come to know the real source location of image file upon clicking its properties. I've seen websites doing this. can somebody...
3
6559
by: Nicolas Castagne | last post by:
Hi all, I have been wondering for a while why function hiding (in a derived class) exists in C++, e.g. why when writing class Base { void foo( int ) {} }; class Derived: public Base { void foo( char const ) {} };
11
5630
by: sofeng | last post by:
I'm not sure if "data hiding" is the correct term, but I'm trying to emulate this object-oriented technique. I know C++ probably provides much more than my example, but I'd just like some feedback to find out if I've done anything wrong. Also, I am working on this for an embedded environment, so if there are great inefficiencies with this...
0
7666
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...
0
7888
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. ...
0
8108
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...
1
7644
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...
1
5484
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...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
925
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...

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.