Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about multiple inheritance

Tags:

c#

.net

oop

I just read some where that C# class can't inherit from multiple class, at the same time I also read that each C# class is inherited from a base class "Object Class".

Now I am confused if I make another class make it to inherit some class, then it is inheriting from the class and the base class, ie two class.

Isn't it breaking the law?

like image 311
Pankaj Avatar asked Nov 15 '25 23:11

Pankaj


2 Answers

Each class inherits from one other class. If you make your own class extend one of your other classes, then it will not directly inherit from Object but rather from your superclass. The superclass in its turn will inherit from Object.

Your subclass will have all the methods from Object available trough the superclass.

This will illustrate it:

void Main()
{
    Console.WriteLine (new Super().GetType().BaseType);
    Console.WriteLine (new Sub().GetType().BaseType);
}

class Super { }
class Sub : Super { }

Output:

enter image description here

like image 124
Jeroen Vannevel Avatar answered Nov 18 '25 14:11

Jeroen Vannevel


This is possible in C#:

enter image description here

(One base class per derived class, aka "single inheritance")

This is not possible in C#:

enter image description here

(Multiple inheritance, use interfaces instead in C#)

like image 33
Uwe Keim Avatar answered Nov 18 '25 13:11

Uwe Keim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!