Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Inheritance and Objects C#

Tags:

c#

oop

Sorry didn't get any funky title for the question.

I had been taught that .Net(C#) doesn't support Multiple Inheritance. But looking at the foo example below I wonder is this really truth ??

class fooList
{
    public int Index()
    {
        return 0;
    }
}    
class foo : fooList
{
    public foo()
    { }
}
class testFoo
{
    void test()
    {
        foo obj = new foo();

        // From object
        obj.Equals(obj);
        obj.GetHashCode();
        obj.GetType();
        obj.ToString();

        // From fooList
        obj.Index();
    }
}

As we can see that I have a class fooList and a class foo which inherits fooList according to the sayings(C# doesn't support Multiple Inheritance) the object of class foo should have only one method which is Index() from the fooList class, But it has more methods from the object class. It clearly indicates that by default all the classes inherit object class. So questions raises

  1. Is it really completely true that C# doesn't support Multiple Inheritance ?
  2. I guess it supports it at CLR level, Than why it doesn't support it in coding ?
like image 676
yogi Avatar asked May 27 '13 06:05

yogi


People also ask

What is multiple inheritance in C?

Multiple Inheritance in C++ C++ProgrammingServer Side Programming. Multiple inheritance occurs when a class inherits from more than one base class. So the class can inherit features from multiple base classes using multiple inheritance. This is an important feature of object oriented programming languages such as C++.

Does C allow multiple inheritance?

In Multiple inheritance, one class can have more than one superclass and inherit features from all its parent classes. As shown in the below diagram, class C inherits the features of class A and B. But C# does not support multiple class inheritance.

Where do we use multiple inheritance?

Multiple inheritance is useful when a subclass needs to combine multiple contracts and inherit some, or all, of the implementation of those contracts. For example, the AmericanStudent class needs to inherit from both the Student class and the American class.

What is inheritance explain multiple inheritance?

Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or class may only inherit from one particular object or class.


4 Answers

C# does not support multiple inheritance of classes at a single level. C# does support multiple levels of inheritance in a hierarchy. What you are seeing is that at the top of your inheritance hierachy is System.Object, which is the top level base class for classes and structs in .NET.

What you cannot have is:

class A { }
class B { }
class C : A, B { }

What you can have:

class A { }
class B : A { }
class C : B { } 

Make sense?

For a bit more completeness, C# does allow you to simulate multiple inheritance via the usage of interfaces. So let's walk it back.

class A { }
interface B { }
class C : A, B { } // legal

In this example, the declaration of C is legal, and you would need to implement the various members that interface B defines as part of C.

like image 106
Anthony Pegram Avatar answered Oct 18 '22 21:10

Anthony Pegram


Multiple Inheritance is not what you're seeing here. Multiple Inheritance means that a class derives from two base classes "in parallel", like maybe this:

public class Test : Base1, Base2 {}

That is not possible.

What you're seeing here (and this works in any object oriented language) is that of course a class A inherits methods and properties from all the entire hierarchy.

For example:

public class Test : Base {}

inherits all methods from Base and Object, if Base is directly derived from Object.

So if you have this hierarchy:

public class Base : Object {}
public class A : Base {}

Base inherits all methods and properties from Object, and A inherits all methods and properties from Base and thus also from Object.

Otherwise it would not be possible to build class hierarchies.

like image 31
Thorsten Dittmar Avatar answered Oct 18 '22 20:10

Thorsten Dittmar


You are showing example of multi label inheritance and not multiple inheritance.

Multiple inheritance means a single class can inherit more than one class.

class A: B,C

Which is not true in case of c#

like image 33
Ishu Avatar answered Oct 18 '22 20:10

Ishu


You are missunderstanding multiple inheritance. A class can inherit a class which already inherits another class. But a class cannot inherit two or more different classes at the same time.

// Default inheritance from Object.
// Class A inherits System.Object
class A     

// Simple inheritance. This is not multiple inheritance.   
// Class B inherits class A, which inherits Object
class B : A     

// Simple inheritance. This is not multiple inheritance.
// Class C inherits class B, which inherits class A, which inherits Object
class C : B  

// Default inheritance from Object.
// Class D inherits System.Object
class D     

// Multiple inheritance
// Class E inherits both class A and class D
class E : A, D

// Multiple inheritance
// Class F inherits both class A and class B
class F : A, B

// Multiple inheritance
// Class G inherits both class C and class D
class G : C, D
like image 31
Mehmet Ataş Avatar answered Oct 18 '22 21:10

Mehmet Ataş