473,729 Members | 1,856 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 1634
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.co m

"Dan" <da************ *@yahoo.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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 similar to what I'm thinking about. Lets suppose I make a class called RoleCollection.
7
8666
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
2200
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 visible to c3! If instead I make c2.foo private, then c3 will access directly c1.foo Thanks, Fabio class c1 { protected void foo() { MessageBox.Show( "foo: c1" ); }
7
5174
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 inherited member by defining a new member with the same signature. This might be done to make a previously public member private or to define new behavior for an inherited method that is marked as final. " However, this does not hide the...
5
2116
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 method, expecting an error or warning; I got neither with the result that the inherited method does _not_ get hidden (i.e. not possibe to break polymorphism/inheritance this way, yay!) My question then is, why is there no warning that the code may...
10
2579
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 the base class properties from showing up in the inherited class when people change its properties in the IDE? thanks!
14
2421
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 project/assembly). I tried with attributes: <Browsable(False), _ EditorBrowsable(EditorBrowsableState.Never), _ RefreshProperties(RefreshProperties.Repaint), _
12
2091
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 with the following: Can this create any problems with GNU g++ compilation, linking etc? What would be the impact to the performance of the system? Anything else we have to consider?
162
10275
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 prefix them with 2 underscores, but I hate prefixing my vars, I'd rather add a keyword before it. Python advertises himself as a full OOP language, but why does it miss one of the basic principles of OOP? Will it ever be added to python?
0
8936
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9436
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9298
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9226
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8170
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4799
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3248
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 we have to send another system
3
2173
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.