473,385 Members | 1,311 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,385 software developers and data experts.

Feature Request: Hiding a property/method in a subclass

To Microsoft or other people who are interested, I have a feature request:

I would really like to have an option to 'hide' a property or method from an
inherited class. For example when I create a subclass and supply a default
value for a property. Currently I create a new property, using the new
keyword, and only supply the get accessor witch will always return the
predefined value. But I would really like to be able to hide it completly.

Currently I do something like this:

class NameOfClass : NameOfBaseClass
{
NameOfClass()
{
base.PropertyName = PropertyName;
}

public new bool PropertyName
{
get
{
// return the default value
return true;
}
// no set method, we don't want it to change.
}
}

But I would realy like to be able to hide the property totally, because it
isn't needed anymore for my custom implementation of the new class/component
Something like:

class NameOfClass : NameOfBaseClass
{
NameOfClass()
{
base.PropertyName = true;
}

hide bool PropertyName;
}

Where the 'hide' keyword (or a 'remove' or 'delete' keyword, whatever)
whould hide the implementation of the base class.
Ofcourse this keyword would work somewhat similar as the 'new' keyword on a
method or property where the base class or code that uses the baseclasses
type, would still see the old implementation.

Is this a good idea, or am I missing some logic why this wouldn't be
possible or maybe a bad thing to implement?


Nov 15 '05 #1
8 7163
Jeroen Smits wrote:
Is this a good idea, or am I missing some logic why this wouldn't be
possible or maybe a bad thing to implement?


Why not just mark the base implmentation as private?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #2
Frank Oquendo wrote:
Why not just mark the base implmentation as private?


If I had control over the base class, ofcourse. But what if you want to
create - for example - a new class based on the
System.Windows.Forms.ListView class or some other control you have no
control over.

I do this a lot, make a subclass on a existing class and supply all kind of
default behaviors and settings. I think hiding properties and or method that
are not supported anymore for the new derived class would be a cleaner
sollution as throwing a NotSupportedException or something like that.
Nov 15 '05 #3
Jeroen Smits <ne**@vortex.nl> wrote:
I would really like to have an option to 'hide' a property or method from an
inherited class.


That would break Liskov's Substitutability Principle, and is a
generally bad thing, IMO. The user would always be able to get round it
by casting to the base type anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
and Liskov's Substitutability Principle is????

--
Regards,
Alvin Bruney
Got DotNet? Get it here...
http://www.networkip.net/dotnet/tidbits/default.htm
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jeroen Smits <ne**@vortex.nl> wrote:
I would really like to have an option to 'hide' a property or method from an inherited class.


That would break Liskov's Substitutability Principle, and is a
generally bad thing, IMO. The user would always be able to get round it
by casting to the base type anyway.

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

Nov 15 '05 #5
<"Alvin Bruney" <vapor at steaming post office>> wrote:
and Liskov's Substitutability Principle is????


A quick web search will answer that much more fully than I'm prepared
to here, but in a few words it is: "You should always be able to treat
instances of a derived class as if they were instances of the base
class."

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
Jon Skeet [C# MVP] wrote:
A quick web search will answer that much more fully than I'm prepared
to here, but in a few words it is: "You should always be able to treat
instances of a derived class as if they were instances of the base
class."


Well then C# already breaks it. If create a subclass and use the 'new'
keyword to redefine a property which had a get/set accessor, to only have a
get accessor, the set accessor gets hidden. I just would like to extend this
possibilty to hide alle accessors (or methods).

Ofcouse one can get around by casting to the base class, but thats not my
point. I just think hide methods can give a clearer implementation of a
class by hiding any methods that will throw NotImplemented exceptions
anyway. And by letting it work in much the same way as the 'new' keyword
code that is using the some baseimplementation won't get broken by it.
Nov 15 '05 #7
<"Jeroen Smits" <jeroen AT bc-br DOT com>> wrote:
A quick web search will answer that much more fully than I'm prepared
to here, but in a few words it is: "You should always be able to treat
instances of a derived class as if they were instances of the base
class."
Well then C# already breaks it. If create a subclass and use the 'new'
keyword to redefine a property which had a get/set accessor, to only have a
get accessor, the set accessor gets hidden.


Yes - something I also don't like, along with explicit interface
implementation.
I just would like to extend this
possibilty to hide alle accessors (or methods).

Ofcouse one can get around by casting to the base class, but thats not my
point. I just think hide methods can give a clearer implementation of a
class by hiding any methods that will throw NotImplemented exceptions
anyway. And by letting it work in much the same way as the 'new' keyword
code that is using the some baseimplementation won't get broken by it.


Hiding methods is always a bad idea to start with though - the reason
it's available (IMO) is to prevent accidental overloading and allow you
to get away with situations which have unfortunately cropped up due to
the base class getting new methods which happen to have the same
signature as the derived class's methods. Using "new" you can get
backwards compatibility, but it will always be unfortunate - it's
certainly not something you should be regularly doing deliberately, and
not something to be encouraged by extending it to other areas.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
Jon Skeet [C# MVP] wrote:
<"Jeroen Smits" <jeroen AT bc-br DOT com>> wrote:
A quick web search will answer that much more fully than I'm
prepared to here, but in a few words it is: "You should always be
able to treat instances of a derived class as if they were
instances of the base class."


Well then C# already breaks it. If create a subclass and use the
'new' keyword to redefine a property which had a get/set accessor,
to only have a get accessor, the set accessor gets hidden.


Yes - something I also don't like, along with explicit interface
implementation.
I just would like to extend this
possibilty to hide alle accessors (or methods).

Ofcouse one can get around by casting to the base class, but thats
not my point. I just think hide methods can give a clearer
implementation of a class by hiding any methods that will throw
NotImplemented exceptions anyway. And by letting it work in much the
same way as the 'new' keyword code that is using the some
baseimplementation won't get broken by it.


Hiding methods is always a bad idea to start with though - the reason
it's available (IMO) is to prevent accidental overloading and allow
you to get away with situations which have unfortunately cropped up
due to the base class getting new methods which happen to have the
same signature as the derived class's methods. Using "new" you can get
backwards compatibility, but it will always be unfortunate - it's
certainly not something you should be regularly doing deliberately,
and not something to be encouraged by extending it to other areas.


Well if they add it or not, for now I'll have to stick with the 'new'
keyword and using the EditorBrowsableAttribute. This way it will at least be
hidden while coding :)
Nov 15 '05 #9

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

Similar topics

21
by: Michele Simionato | last post by:
I often feel the need to extend the string method ".endswith" to tuple arguments, in such a way to automatically check for multiple endings. For instance, here is a typical use case: if...
3
by: Jules Dubois | last post by:
I'm want to create a superclass with nothing but attributes and properties. Some of the subclasses will do nothing but provide values for the attributes. (I'd also like to make sure (1) that the...
14
by: Stephan Diehl | last post by:
I was playing around with defining new types and have seen the following behaviour: Python 2.3.1 ============ >>> class Int(int):pass .... >>> a = Int(7) >>> b = Int(8)
16
by: Michele Simionato | last post by:
I have read with interest the recent thread about closures. The funny thing is that the authors are arguing one against the other but I actually agree with all of them and I have a proposal that...
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...
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
12
by: Raymond Hettinger | last post by:
I am evaluating a request for an alternate version of itertools.izip() that has a None fill-in feature like the built-in map function: >>> map(None, 'abc', '12345') # demonstrate map's None...
8
by: SpotNet | last post by:
Hello NewsGroup, I have a base class and six classes that inherit from this base class. All members in the base class are used in it's extended classes except, in one of the extended class one...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.