Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 579
  • Last Modified:

multiple inheritance in C#?

Could anyone give me a simple example of  a multiple inheritence in C#?
I have searched online but most of the web sites who claim they do multiple inheritance using interfaces still need to create an instance of an object in order to access the object's properties. For examples if this could be done is C++, how could i do in C#?
I just made up this examples to show that i could inherit base's class properties without creating an instance of an object. In C# i could do this but with only one inheritance.
Thanks in advance,
 

public class A
{
    private int myInt=1;

    public int getInt()
    {
       return myInt;
    }
}

public class B
{
    private string myStr="Hello";

    public string getStr()
    {
        return myStr;
    }
}


public class C: A, B
{
    public void Main()
    {
       int newInt= base. getInt();
       string newStr=base.getStr();
    }
}
0
Carl3003
Asked:
Carl3003
3 Solutions
 
stanscott2Commented:
C# doesn't support inheritance; I think the next version will, but I'm not certain.  In this article, the author discusses ways around this limitation:
http://www.c-sharpcorner.com/Language/MultipleInheritanceCB.asp

SS
0
 
Mohammad Aamir QureshiCommented:
check this example

public class Machine
{
    /// Your code
}

public class Bird
{
    /// Your code
}

public class Aeroplane: Machine , Bird
{
   /// Your Code
}

Basically inheritence show "IS A"  relation ship so for above example

Machine IS A Aeroplane  and
Bird IS A Aeroplane

CHEERS
0
 
Mohammad Aamir QureshiCommented:
check this multiple inheritance also

public class bridge
{
       // Bridge functionality Code
}

public class hub
{
    //  hub functionality Code
}

public class Switch : bridge , hub
{
   // Your code
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Mohammad Aamir QureshiCommented:
Well

C# does not support multiple inheritance but it support inheritance Above example is multiple inheritance in C++
now every object oriented programming doesnt support multiple inheritance

TaKe CaRe
0
 
nachiketCommented:
Without Creating any instance it is not possible in C# to implement multiple inheritance.

Fundamentals of OOAD design says it is not a good design to use multiple inheritance!!

Since IS a relationship is only one!! I agree some cases can be argued but if we design properly we will have a way out!!

Here is one of the options you have. If see the class c which u asked has the same implementation!!

===============================
      public interface IA
      {             int getInt();      }
      public interface IB
      {            string getStr();      }

      public class BaseClassOfAB
      {
            public IA AObject;
            public IB BObject;

            public BaseClassOfAB(IA aObj,IB bObj)
            {
                  AObject = aObj ;
                  BObject = bObj;
            }
            public int getInt()
            {                  return AObject.getInt() ;            }

            public string getStr()
            {                  return BObject.getStr() ;            }
      }
      public class C: BaseClassOfAB
      {
            public void Main()
            {
                  int i= base.getInt();
                  string s = base.getStr();
            }
      }
0
 
TiexCommented:
You can specify only one class as a parent class but you can specify any amount of interfaces as parents.
0
 
Carl3003Author Commented:
Thanks for all of you. I dont get one thing. If its the case that multiple inheritance in c# cant be done withouth making instances than what is the point of the interfaces? I could create abstact classes with protected functions and the effect will be the same, right? The reason why i would not go with instances is because most of the time i work locally in functions and everytime i want to access a base class propery i will have to make an instance. Or if i modify that base class' propery than i would need to send an i reference if i need to use this base class in somewhere else . I guess this is the type of design i should look for .
0
 
gregoryyoungCommented:
The main differentiation between multiple inheritance and single inheritance is when it comes to bases.

In multiple inheritance you can inherit as many bases as you like, many say that a good design will lead to many purely abstract bases and a single non-purely abstract base.

Due to the complications that can arise in multiple inheritance based design single inheritance was developed to lower the learning curve.

In single inheritance you can only inherit from one base, period. You are given the option of implementing multiple interfaces, interfaces are nothing more than another word for a purely abstract base. By only allowing you to implement interfaces, single inheritances forces you to honor the "best practice" of only inheriting multiple purely abstract bases making design a bit easier but also taking away significant functionality.

I personally have always preferred multiple inheritance as putting default functionality on your bases can save alot of code (you will quickly find this when dealing with interfaces)

As for your mentioning of needing an instance to deal with an interface, this is true for C# however it is NOT true for the CLR. You can create static members boung to an interface type in IL ... this will allow you to do something such as the pseudocode below (note it will not compile as it is not allowed in C#).

public interface IFoo {
    bar {get;}
    static void Count() {
         Console.WriteLine("IFoo::Count");
    }
}

you would then be able to use the method as you can with a base class in C# ...

IFoo.Count() //method bound to type

If you like, I can show you how to implement this in IL but I would tend to lean against writing such code in a "real world" application ...

Greg
0
 
nachiketCommented:
Interface are a special way and has a special place in OOAD

as we know "Aggregation/Composition" is "HAS A" relationship ... e.g. Car has a engine
and             "Inheritance" is "IS A" relationship ... e.g. Crow is a Bird

However Interfaces are something called "Behaves Like" relationship... This behaves like relationship can be implemented to get some relation ship between 2 unrelated classes... e.g.1  Car Moves and Animal Moves. They way Car and Animal Moves are different. so one option to design this is to create a Move class and Animal and Car inherit from it.. But if u think a bit, it is not good. Because it is not "Is a"  relationship. Car is not a Move and neither Animal is Move. However Car and Animal can implement IMovable interface. So car has a behavior of Moving. Similarly Animal has behavior of Moving.

Interfaces can also be used to design by contract. E.g. Car has a Lock and Home has a Lock both the locks are different. So we can not have a common base class and implement the lock there. So we can define an abstract base class and an abstract lock. So both Car and Home needs to implement their own lock in a specific way. However to implement this requirement we do NOT need a class. We just want to make sure you want a lock. An interface can do that work for u. And you can implement any interface!! So the limitation of single inheritance is also not a problem for u. you still have some other class to inherit from!!

<<Your Point>> Thanks for all of you. I don’t get one thing. If its the case that multiple inheritance in c# cant be done without making instances than what is the point of the interfaces?

<<Explanation>> Agree that it can not be done in C#. However please keep in mind any OOAD good practice will tell you to prefer composition over inheritance. So if we design properly we will rarely get a need for multiple inheritance. interfaces are not only used to facilitate multiple inheritance. They are there to design by contract!!! in above example i have given one of the scenarios where it can be useful. only thing with C++ is when u use multiple inheritance it will create objects of all the base classes automatically and here in C# we have to create it!! however remember that it is NOT a good practice to use multiple inheritance. will lead you to problems some day when your application grow.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now