Basic question about inheritance/hiding methods

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

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?
 
Dave said:
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.
 
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.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
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.
 
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
 
Back
Top