473,497 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hiding inherited classes

Dan
I have a need to make a set of classes that all share the same public
methods, some implementation and some data. So, I made an abstract
base (BaseClass) with an interface (IBaseClass) and a handful of
inherited classes. The static method calls the ctor of the appropriate
inherited class and returns it as type IBaseClass. There are no new
methods or properties in the inherited classes and public clients never
need to know which inherited class they have. From reading some posts
on this board I think I may have gone the wrong way here. Maybe I
should make BaseClass not abstract and plug in classes representing
different implementations. The code would not be all that different
(rearrange some stuff) because the plug-ins would be mostly the same as
the inherited members now. So, I'm curious if there are any design
gurus who would like share an opinion on this.

Current Design (drastically simplified)

interface IBaseClass
{
void DoStuff();
}

public abstract BaseClass : IBaseClass
{
static IBaseClass MakeIBaseClass(string behaviorInfo)
{
switch (behaviorInfo)
{
case "A":
return new InhA() as IBaseClass;
break;
case "B":
return new InhB() as IBaseClass;
break;
}
public void DoStuff()
{
//Do common stuff
_DoStuff()
}
protected abstract void _DoStuff()
}

And to keep this from going on forever, I'm sure you can see the
inherited classes (InhA, InhB, etc.) would have their own
implementations of _DoStuff. They also override some public base
properties, hold some of their own data and have different ctors.

Should I go to a plug-in (I guess delegation/containment) design over
this? Any advice would be greatly appreciated.

TIA,
Dan

Nov 17 '05 #1
4 1620
Dan,

I have this exact problem, which I believe to be a slight shortcoming in
the language. I don't like the idea of exposing public methods either when
I am implementing an interface, and I have tried to use the same pattern
that you are trying to use now.

There are only two ways to get around it. The first is what you are
doing, having the interface implementation call a protected virtual method
which is overriden by the derived class. I think that this is the best
solution for what you want to do.

The second is to actually expose the method as an implicit interface
implementation, marking it as virtual on the base class. This way, you can
override it on the derived class, and still have the interface
implementation call that method.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan" <da*************@yahoo.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I have a need to make a set of classes that all share the same public
methods, some implementation and some data. So, I made an abstract
base (BaseClass) with an interface (IBaseClass) and a handful of
inherited classes. The static method calls the ctor of the appropriate
inherited class and returns it as type IBaseClass. There are no new
methods or properties in the inherited classes and public clients never
need to know which inherited class they have. From reading some posts
on this board I think I may have gone the wrong way here. Maybe I
should make BaseClass not abstract and plug in classes representing
different implementations. The code would not be all that different
(rearrange some stuff) because the plug-ins would be mostly the same as
the inherited members now. So, I'm curious if there are any design
gurus who would like share an opinion on this.

Current Design (drastically simplified)

interface IBaseClass
{
void DoStuff();
}

public abstract BaseClass : IBaseClass
{
static IBaseClass MakeIBaseClass(string behaviorInfo)
{
switch (behaviorInfo)
{
case "A":
return new InhA() as IBaseClass;
break;
case "B":
return new InhB() as IBaseClass;
break;
}
public void DoStuff()
{
//Do common stuff
_DoStuff()
}
protected abstract void _DoStuff()
}

And to keep this from going on forever, I'm sure you can see the
inherited classes (InhA, InhB, etc.) would have their own
implementations of _DoStuff. They also override some public base
properties, hold some of their own data and have different ctors.

Should I go to a plug-in (I guess delegation/containment) design over
this? Any advice would be greatly appreciated.

TIA,
Dan

Nov 17 '05 #2
Dan
Thanks Nicholas. It helps to have someone confirm I haven't gone
completely sideways.

I would like to see a way to override protected abstract/virtual
methods of a base class in a more private way. Kind of a reverse
protected. Where an override method in a child class could be seen
only by the virtual/abstract member of the parent class. I'm sure that
is bad OO design or something. Basically trying to improve
encapsulation (exposing members only to those related classes that need
them) through more private inheritance. In the same vein I was excited
to see that in 2.0 I can give different protection levels to gets vs.
sets in a property. If I have to write a get, I want to restrict it as
much as possible.

Thanks again,
Dan

Nov 17 '05 #3
Dan,

I think that marking the leaf classes with the "internal" access
modifier would sort of achieve most of what you want.

I suppose you could als go a little farther and use the strategy
pattern.

Nov 17 '05 #4
I would get rid of the switch and use a dynamic class factory.

Regards,
Jeff
switch (behaviorInfo)
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #5

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

Similar topics

6
389
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...
7
8641
by: Tron Thomas | last post by:
Under the right compiler the following code: class Base { public: virtual void Method(int){} }; class Derived: public Base {
4
2191
by: Fabio Cannizzo | last post by:
Is there a way to hide an inherited protected member so that it is no longer accessible nor visible from the inherited classes? The code below makes c1.foo() no longer accessible, but c2.foo is...
7
5150
by: Dennis | last post by:
I have a class named myclass that inheirits from "baseclass". There is a property of "baseclass" that I don't want exposed in the IDE. The MSDN documentation says" "A derived type can hide an...
5
2103
by: PIEBALD | last post by:
I was trying to break some polymorphism, expecting it not to work, but I'm a curious sort. I was seeing what happens when a derived class tries to hide an inherited method with a private new...
10
2552
by: Smokey Grindle | last post by:
i want to inherit the list view class, but in the inherited class, hide the Header style property and the view property (basically its a detailed list with always clickable headers) how do I keep...
14
2384
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...
12
2058
by: Janaka Perera | last post by:
Hi All, We have done a object oriented design for a system which will create a class multiply inherited by around 1000 small and medium sized classes. I would be greatful if you can help me...
162
10061
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
0
7121
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,...
0
6993
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7197
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...
1
6881
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...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4899
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1411
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 ...

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.