sithanathan

Hi Guys,

Multiple inheritnce not supported in C# But we can achieve this using interface right.

plz find the code below.I have implemented multiple inheritance using interface

public interface coolbaby1

{

void caller();

}

public interface coolbaby2

{

void caller();

}

public class Multiple : coolbaby1, coolbaby2

{

void coolbaby1.caller()

{

}

void coolbaby2.caller()

{

}

}

As per the multiple inheritance concept i have implemented my Muliple class derived from two interfaces cool baby1,cool baby2

But inheritance means code reuseability right here we are not getting any code reuseability then

My question how come we are telling multiple inheritance possible through interface without code reuseablity....

kindly help me.....



Re: Visual C# Language Reg:Multiple inheritance

Gregor Berginc

Note that its not just multiple inheritance, but multiple interface inheritance. I agree with you that there are occasions when you would like to inherit from more than one class. But, to be honest, this does not happen that frequent and can always be solved with inheritance of interfaces. Consider the following code:

Code Snippet

public interface IA

{
// ...
}

public interface IB
{
// ...
}

public class A : IA
{
// Implementation of IA
}

public class B : IB
{
// Implementation of IB
}

public class C : B, IA
{
private A a;

// Implementation of IA using object a.
}



Now you can reuse class A, although it is not as nice as it would be if multiple implementation inheritance was allowed.

Read http://blogs.msdn.com/csharpfaq/archive/2004/03/07/85562.aspx for further information.

cheers,





Re: Visual C# Language Reg:Multiple inheritance

michhes

In a Java world (like C#, Java only supports single inheritance), you use the extends keyword and write something like this:

Code Snippet

public class Student extends Person
{
}

Whereas you use the implements keyword to implement an interface.

As you already know, C# isn't as verbose when it comes to inheriting from a base class or implementing an interface but it's still usefull to use the Java keywords in your head as you go...

You quite rightly pointed out that implementing an interface does not offer the code reuse you would get by inheriting from a class. An interface isn't supposed to provide any impelmentation details but only describes the public 'interface' (behaviour) implementing classes must provide. An interface also declares a type so in this case, while not getting true multiple inheritance, an instance of the Multiple class is obviously of type Multiple but it is also of type coolbaby1 and coolbaby2.

BTW the Multiple class is implementing two interfaces; the interfaces aren't inheriting from each other (which is possible) so strictly speaking, this isn't an example of interface inheritance.

In answer to your question, multiple inheritance is not possible in C#.






Re: Visual C# Language Reg:Multiple inheritance

siddharthan

Dear Gregor Berginc,

Thanks.....

I have tried as per your code...

I'm not understaning Implementation of IA using object i given my sample code plz highliht from that plz....

interface IA

{

void Show1();

}

interface IB

{

void Show2();

}

class A : IA

{

public void Show1()

{

Console.WriteLine("Show() method Implemented");

}

}

class B : IB

{

public void Show2()

{

Console.WriteLine("Show2() method Implemented");

}

}

class C : B,IA

{

public static void Main(string[] args)

{

//your comments required here how to achieve code reuseability

}

}





Re: Visual C# Language Reg:Multiple inheritance

siddharthan

Dear michhes,

so multiple inheritance is not possible in C#....

everyone telling like we can achieve multiple inheritance using interfaces that is possible or not

If possible how to do that plz let me know





Re: Visual C# Language Reg:Multiple inheritance

timvw

What's the use of starting the same thread in multiple forums

(eg: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1607547&SiteID=1)





Re: Visual C# Language Reg:Multiple inheritance

Gregor Berginc

In your case, you can invoke Show2 method of class B (since C inherits from B). But, class A cannot be inherited as well. So you can simulate A's method Show like in example below:

class C : B, IA
{
private A a;

public C()
{
this.a = new A();
}

// Here, you have to implement interface IA
public void Show()
{
this.a.Show();
}

public static void Main(string[] _args)
{
C c = new C();

// Although c does not inherit from A, you can simulate Show() method, using c:
c.Show();
}
}

Once again, this does not mean that C inherits from A! It is just a simulation of inheritance!

I really hope this helps you.





Re: Visual C# Language Reg:Multiple inheritance

siddharthan

Dear Berginc,

thanks I got your explanation.....





Re: Visual C# Language Reg:Multiple inheritance

siddharthan

thanks lot......



Re: Visual C# Language Reg:Multiple inheritance

michhes

You're not achieving multiple inheritance by implementing an interface, you're telling the compiler your class provides the public methods declared in the interface. You're class isn't inheriting any of the code implementation from the interfaces because an interface does not provide an implementation for the methods it declares.

You can implement multiple interfaces. You can't inherit from multiple classes.






Re: Visual C# Language Reg:Multiple inheritance

Stefan Van Reeth

If u really can't live without multiple inheritance, I suggest u switch to C++. I'm sure u're gonna love it Wink.

That aside, there are some reasons why newer languages like Java en C# don't allow it.

First of all, needing multiple inheritance generally (or even: in all cases ) can be traced back to bad design. In all the years I've coded, I can't imagine a case where I ever felt the need for it.

Secondly, it introduced a whole bunch of problems that drove people crazy. Like having the same method signature in both classes where u inherited from.

By the way, if u need more explanation on the subjects above, I'd keep to C# and live with it. No pun intended, but people who are vastly more experienced are actually glad to see those constructs gone.