How can I hide base class members that are public

  • Thread starter C-Sharper or C-Hasher, one of the two
  • Start date
C

C-Sharper or C-Hasher, one of the two

Hi,

I have a C# class libary (Class1) which is inherited by another class in a
Windows Forms app. I want this other class to implement some, but not all of
the features of the base class. How can I completely hide (within the IDE)
the members of the base class library i.e. only expose those members that I
want exposed? I've tried some methods gleaned by googling for "hiding base
class members" which suggested using things like "new" and a blank function
structure but this doesn't apparently work. The only idea I've come up with
is not to inherit the base class but simply create an instance of it and use
that instead and then create only the methods and properties that I need in
my derived class. This at the moment seems to be the most logical solution,
but it also seems like a lot of extra work, does anyone know of a better
way. I basically want it to work like using interfaces, you know, expose and
hide what you want as appropriate.

Thanks

Greg
 
M

michael

Can you not just make Class1 an interface and make those methods that you
wish not be to re-implemented as "private"? Nonetheless, it really sounds
like you should be moving those common methods to an interface of its own
(e.x ClassInterface) and then have Class1 implement on that common class
(Class1 : ClassInterface).

Hope this helps.
 
C

C-Sharper or C-Hasher, one of the two

Hi Michael,

I don't think I can make Class1 an interface as it is a fully useable class
by itself. Class2 builds upon it and hides some of it's unused features for
this specific application. Your thoughts?

Regards

Greg
 
J

Jon Skeet [C# MVP]

C-Sharper or C-Hasher said:
I have a C# class libary (Class1) which is inherited by another class in a
Windows Forms app. I want this other class to implement some, but not all of
the features of the base class. How can I completely hide (within the IDE)
the members of the base class library i.e. only expose those members that I
want exposed?

You can't. If you don't want people to have access to the base class's
members, don't inherit from it. Have a search for Liskov's
Substitutability Principle for good reasons for this.
I've tried some methods gleaned by googling for "hiding base
class members" which suggested using things like "new" and a blank function
structure but this doesn't apparently work. The only idea I've come up with
is not to inherit the base class but simply create an instance of it and use
that instead and then create only the methods and properties that I need in
my derived class. This at the moment seems to be the most logical solution,

Yup, it is.
but it also seems like a lot of extra work, does anyone know of a better
way. I basically want it to work like using interfaces, you know, expose and
hide what you want as appropriate.

Composition can be a bit of extra work, but gives a lot of flexibility
and can avoid unintended uses of your class.
 
C

C-Sharper or C-Hasher, one of the two

Hi Jon,

Thanks for clearing that up for me. Some drudge work to do, but not too
much of a problem, at least I don't have to rewrite the implementation
stuff. I have to admit to not having every heard of "Liskov's
Substitutability Principle". Just had a quick look on Google for it and
found a simplified explanation. Which explains it well enough.

Regards

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top