Click here to Skip to main content
14,869,990 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I saw many examples saying Multiple Inheritance can be implemented by Interface. But all those creating me confusion that, Using Interface we are implementing the all the methods(cant see reuse) where as in Inheritance we can reuse existing methods(functionality). So will Interface really support Multiple Inheritance?

Ex:
C#
public interface IA
{
  string IAMethod();
}
public interface IB
{
  string IBMethod();
}

public class C: IA, IB   // Here we are only implementing and No Reuse (Ihneritance?)
{
  public string IAMethod()
  {
    return "IA method called";
  }
  public string IBMethod()
  {
    return "IB method called";
  }

}

Please provide if anybody having best example that clears my confusion...
Posted
Updated 27-Jun-12 20:07pm
v3

Interface offers multiple inheritance.
This[^] article may help you.

If you search on the internet, you will get tons of more information and articles.
   
Comments
   
Correct, my 5, but it requires additional note I've made. Please see my answer.
--SA
Abhinav S 28-Jun-12 2:09am
   
Thank you SA.
   
Comments
   
It answers the question, my 5. I've added my answer and credited yours, but this is mostly to make Solution 1 more accurate -- on the page you reference it's explained.
--SA
Abhinav S 28-Jun-12 2:10am
   
Yup. 5.
In addition to the correct Solution 1 (Solution 2 actually confirms it):

Not only interfaces are subject to multiple inheritance, but a class or a structure (sic!) can implement more then one interface (but a base class can be only one). Sometimes this is called "weak form of multiple inheritance".

—SA
   
v2
Comments
Abhinav S 28-Jun-12 2:09am
   
Agreed. 5.
   
Thank you, Abhinav.
--SA
In addition to the correct Solution 1 (Solution 2 actually confirms it):

Not only interfaces are subject to multiple inheritance, but a class or a structure (sic!) can implement more then one interface (but a base class cannot be only one). Sometimes this is called "weak form of multiple inheritance".

—SA
   
Comments
sarath.ms 28-Jun-12 3:02am
   
Hi SA, thanks for ur respone. For ex, i have a sinerio like this,
public class HP: Printer
{
string getToner()
{
}
}

public class Canon:Printer
{
some methods
//I need to reuse the HP's method - getToner()
}
Can above sinerio be done without creating object to HP?
Sergey Alexandrovich Kryukov 29-Jun-12 18:22pm
   
Yes, but if you want to reuse this method, move it to some common ancestor class, the class Printer or create some intermediate class. Do you know how virtual functions work, by the way? How is this related to multiple inheritance? To interfaces? If does not look like you need them, but the accurate judgement could be only done based on the complete type hierarchy...
--SA
wizardzz 28-Jun-12 11:46am
   
Repost.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900