Multiple Inheritence.

Started by
5 comments, last by DarkScience 11 years, 11 months ago

What I am trying to accomplish is a form of inheriting multiple classes, I tried interfaces but they are rather limited, then i tried abstract classes. Everything seemed fine but as it turns out (as i forgot) a class can only have 1 base class.
Is there a way to have a bunch of base classes and have them inherit/ be inherited ?
Below is 2 of the class files im trying to inherit.

  • interface - FAILED, cant enable external acces (public) for members, cant use virtual, cant specify default values
  • class (abstract) - FAILED
  • layer inheriting this inherits this which inherits that, i know this is a bad idea and wouldnt work. - NOT A GOOD IDEA , FUNDAMENTALLY FLAWWED

[SPOILER]

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace game

{

public class ILogical

{

public bool ShouldLogic = true;

public void Logic( float mul ) { if ( ShouldLogic ) OnLogic ( mul ); }

public virtual void OnLogic( float mul )

{

}

}

}

[/SPOILER]
[SPOILER]
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace game

{

public class IRenderable

{

public bool ShouldRender = true;

public void Render( ) { if ( ShouldRender ) OnRender ( ); }

public virtual void OnRender( ) { }

}

public class IDrawable : IRenderable

{

public bool ShouldDraw { get { return ShouldRender; } set { ShouldRender = value; } }

public void Draw( ) { Render ( ); }

}

}

[/SPOILER]

Also found this, currently trying to make sense of it.
http://msdn.microsof...y/64hstbtx.aspx
Advertisement
In C# you use interfaces instead of inheriting from multiple base classes. What is it that you're trying to do? If you wind up with multiple inheritance in your design, you should try to rethink your design more often than not.
The link you found is for C++, in which multiple inheritance is allowed. It's not allowed in C#.
My end goal is being able to inherit varius members from varius files without haveing to retype. Along with being able to use casting to invoke/access. where interfaces fail is the inability to specify values such as "public bool ShouldDoStuff = false;" or even specifying the members to be public/private. I will probably adjust my design model to something that uses interfaces. I'm just wondering if i can still assign values, use virtual, ect ...

Thought it appears not so nuts to me, Time to get complicated or sloppy.
An interface only defines the behavior of a class, not the state. In other words, an interface doesn't (can't) have variables, only methods. It's basically a class consisting of only abstract methods, with the distinction that you can implement several interfaces, where you can only extend one class.
Well this sucks, oh well. Guess i'll specify these things on a per class basis.
Though 1 more thing.

If i have a class with.
public String SomeVariable = "";
and i cast another class that has that property defined to this one.
will i be able to access its value ? (acces classA's version of the value ?)

ClassA : ClassB
If Bar extends Foo, and you have a variable a of type Bar, you can cast it into a Foo type. You need the inheritance, not just the same fields.
My advice is to rethink your design, it sounds as if you could maybe fashion something less complicated.
Alright thanks for your help.

This topic is closed to new replies.

Advertisement