Reputation: 744
I have the classes A and B where B inherits from A. Both classes are abstract and implement some methods and some methods are abstract, forcing the specialized implementation to implement those functions.
Now, specialized A (specA) inherits from A and specialized B (specB) inherits from B. As a result (tried in C#) it seems like specB does not inherit from specA - specA test = specBInstance; won't work (which makes sense, because specB does not inherit from specA...). Question is how to design the whole thing, so that specB at least acts like it would directly inherit from specA?
The result should be like this - it is like spezializing a whole hierarchical construct and not only one class...
A --<-- specA
|
^ ^ need inheritance here
|
B --<-- specB
Upvotes: 0
Views: 56
Reputation: 25638
C# doesn't support multiple inheritance.
You should probably look into favouring composition over inheritance. The functionality of specA that you want specB to have access to can be broken out as a separate class and injected where required.
Reading your comment about the CAD models, you can make use of something like the 'Strategy Pattern' - break out your functions as separate classes. There are many different variations on the code, but you can achieve things like this:
public class Animal
{
// In this case we pass the sound strategy to the method. However you could also
// get the strategy from a protected abstract method, or you could even use some sort
// of IOC container.
public void MakeSound(SoundStrategy soundStrategy)
{
soundStrategy.MakeSound();
}
}
public class Bark : SoundStrategy
{
public override void MakeSound()
{
Console.WriteLine("Woof");
}
}
public class Meow : SoundStrategy
{
public override void MakeSound()
{
Console.WriteLine("Meow");
}
}
public class BarkLoudly : Bark
{
public override void MakeSound()
{
Console.WriteLine("WOOF");
}
}
Upvotes: 2