Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

Basic question about inheritance/hiding methods

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dave - 26 Mar 2006 01:20 GMT
I have a C# collection class that inherits from SortedList.  I want to be
able to hide certain methods such as Add.  How do I do that?
Jon Skeet [C# MVP] - 26 Mar 2006 08:26 GMT
> I have a C# collection class that inherits from SortedList.  I want to be
> able to hide certain methods such as Add.  How do I do that?

You can't. That would break Liskov's Substitutability Principle.

If you want to expose only part of a class's public interface, you
should use composition/aggregation, rather than inheritance.

You *could* just override Add to throw an exception - but that would be
bad form, as anyone who received your object as a SortedList would
expect to be able to add to it.

Signature

Jon Skeet - <skeet@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

Kevin Spencer - 26 Mar 2006 16:28 GMT
To elaborate on Jon's reply: Create a new class that does not *inherit*
SortedList, but *contains* a SortedList instance in it. You can expose the
attributes of SortedList via the new class, and not expose those which you
want to hide.

In addition, you may want to implement some of the interfaces that
SortedList implements, for a variety of reasons.

I recently had a similar problem when creating a Generic HistoryList
Collection. There is no such Collection in the Framework. A HistoryList is a
Collection which has a limited capacity, and can grow to that capacity, but
no larger. When it reaches Capacity, it removes items from the beginning of
the list to make room for new ones. In addition, it has a Position, which
indicates the current Position in the list, to allow for both undoing and
redoing. One can move the Position backwards and forwards in the list. When
one adds an item at a Position which is not the end, all items from that
point forward are removed.

I looked at a number of different Generic Lists, Collections, and
Interfaces, and determined that the Generic List was my best place to start.
I created the List as a protected member, and exposed the method, properties
etc., which were relevant while hiding others. I then added methods,
properties, etc., which provided the "HistoryList-specific" functionality
required. I also implemented the IEnumerable, ICollection, and IList
interfaces, to make it useful in a variety of situations which require
interfaces.

Signature

HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

>> I have a C# collection class that inherits from SortedList.  I want to be
>> able to hide certain methods such as Add.  How do I do that?
[quoted text clipped - 7 lines]
> bad form, as anyone who received your object as a SortedList would
> expect to be able to add to it.
Dave - 26 Mar 2006 19:19 GMT
Thanks for the detailed replies.

Aggregation/composition will be the way to go.  This collection needs to be
remotable anyway so I'm already providing methods on another class to do
things like Add.
profdotnet - 26 Mar 2006 08:40 GMT
Use New keyword in C# to hide inherited methods from  base class.

Check out this link.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclr
fnewoppg.asp


> I have a C# collection class that inherits from SortedList.  I want to be
> able to hide certain methods such as Add.  How do I do that?
Jon Skeet [C# MVP] - 26 Mar 2006 08:51 GMT
> Use New keyword in C# to hide inherited methods from  base class.
>
> Check out this link.
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclr
fnewoppg.asp

That doesn't *really* hide the inherited members though - it just
allows you to create new members with the same name which aren't
overrides of the "old" members. It's not the same as preventing the
"old" members from being available.

Signature

Jon Skeet - <skeet@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

n4ixt - 27 Mar 2006 13:28 GMT
You can't. Inheritance doesn't allow you to do that.

What you could do is write your own class that does not inherit, but does
have a SortedList inside it. You will then have to implement all of the
properties and methods of the SortedList that you want exposed, and simply
not implment the ones that you don't want to have, like Add. A lot more
work, but it would meet your goal.

The second thing you could do is implement your own Add method, overriding
the one in SortedList. Only instead of doing anything it simply pops up a
message that says "Add is not allowed in this class" (or throw an error or
something like that). Much quicker and easier to implement, it prevents the
Add from working in your class.

  Robert

>I have a C# collection class that inherits from SortedList.  I want to be
>able to hide certain methods such as Add.  How do I do that?
sloan - 27 Mar 2006 21:37 GMT
The adapter pattern is along this lines.

http://www.dofactory.com/Patterns/PatternAdapter.aspx

This allows you to only provide the functionality you desire.

There is also the
http://www.dofactory.com/Patterns/PatternFacade.aspx
Fascase pattern.

The differences are subtle , but there exist.

> I have a C# collection class that inherits from SortedList.  I want to be
> able to hide certain methods such as Add.  How do I do that?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.