Pankaj
Pankaj

Reputation: 1524

Confused about multiple inheritance

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?

Upvotes: 4

Views: 348

Answers (7)

Eric Lippert
Eric Lippert

Reputation: 660068

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. Now I am confused.

Every C# class except object inherits directly from exactly one other class. Object inherits from no class.

A C# class can inherit indirectly from any (non-negative!) number of classes.

class Animal : Object {}
class Giraffe : Animal {}

Are our rules met? Yes. Object inherits from no class. Animal inherits directly from Object and indirectly from no class. Giraffe inherits directly from Animal and indirectly from Object.

This eliminates the contradiction.

Upvotes: 7

bas
bas

Reputation: 14912

It does not break the rule.

Every class in C# will inherit from exactly one class. That class either is any class the author of that class wants to extend, or, if none specified, it will extend the Object class.

public class MyClass {} // extends from default Object class

would be exactly the same as

public class MyClass : object {}

Or, when the author wants to extend a specified class:

public class MyClass : AnyBaseClass {}

Upvotes: 0

Uwe Keim
Uwe Keim

Reputation: 40736

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#)

Upvotes: 10

Thomas Weller
Thomas Weller

Reputation: 11717

Multiple inheritance means that a class' inheritance tree can be branched as desired (i.e. it can inherit from more than one base classes at the same time). That's (thank god) not possible in C#.

But as long as the inheritance tree has always only one 'upper leg', it can be as deep as required (i.e. there is something like an inheritance 'chain', where one class inherits from the other).

Upvotes: 0

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

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

Upvotes: 10

Niels Keurentjes
Niels Keurentjes

Reputation: 41958

Every class, except Object itself, will implictly extend Object if it does not extend another class. Thus all classes implictly always eventually extend from Object.

The only common programming language supporting multiple inheritance is C++, it's explictly not allowed in most languages for good reason.

Upvotes: 1

Jite
Jite

Reputation: 5847

One class can inherit from one other class directly and implement multiple interfaces.
You can chain inheritance, eg class a inherits from class b while class c inherits from class a.

interface i {}
class a {}
class b : a {}   // OK
class c : i,b {} // OK
class d : a,b {} // Not OK

Upvotes: 0

Related Questions