Avoid inheriting base class to more than one class

V

VijayRama

Hi,

How to avoid deriving a class to more than one class?
For example consider I have a Class A ( base class) which is derived
by Class B. Now if I try to derive Class A to Class C it shouldn't be
allowed. How can I do this?

class A
{
}
class B:class A
{
}
class C: class A // deriving class A should not be allowed
{
}

Thanks in advance for your help.
 
A

Andrew Baker

You could put something together that prevents you creating instances of
more than one derived class at runtime. I can't think of a way to stop
you writing more than one derived class.

The main question, though, is why on earth have you got this
requirement? Methinks your design is probably (very) wrong..
 
R

RayLopez99

Hi,

How to avoid deriving a class to more than one class?
For example consider I have a Class A ( base class) which is derived
by Class B. Now if I try to derive Class A to Class C it shouldn't be
allowed. How can I do this?

class A
{}

class B:class A
{}

class C: class A // deriving class A should not be allowed
{

}

Thanks in advance for your help.

The compiler will stop you if your classes are circular.

As for: class C: class A // deriving class A should not be allowed

Just make a mental note not to do that when you code. If you mean
during run time, then simply don't make C depend on A--presto! you
are done.

RL
 
A

Arne Vajhøj

The compiler will stop you if your classes are circular.

True, but that is not the case here.
Just make a mental note not to do that when you code. If you mean
during run time, then simply don't make C depend on A--presto! you
are done.

That is a simple solution that may or may not work.

Arne
 
A

Arne Vajhøj

How to avoid deriving a class to more than one class?
For example consider I have a Class A ( base class) which is derived
by Class B. Now if I try to derive Class A to Class C it shouldn't be
allowed. How can I do this?

class A
{
}
class B:class A
{
}
class C: class A // deriving class A should not be allowed
{
}

What is is that you want?

Do you want to prevent more than one type of A to be
instantiated within a running program?

Try a run time check a la:

public class A
{
private static Type sub = null;
public A()
{
if(sub == null)
{
sub = this.GetType();
}
else if(sub != this.GetType())
{
throw new Exception("No no my friend");
}
}
}

Do you want to prevent more than one type to inherit from
a public class A in an assembly?

Can not be done. If I get a copy of that assembly on my PC I can inherit
as much as I want.

Do you want to prevent more than one type to inherit from
an internal class A within the same assembly?

You can not make a compile time check. But you can make a runtime
check similar to the above to prevent instantiation of multiple
subclasses.

Do you have a class that you only want to allow inheritance
from in some specific assemblies?

Make it internal and use the InternalsVisibleToAttribute.

Arne
 

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