Reputation: 1524
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
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
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
Reputation: 40736
This is possible in C#:
(One base class per derived class, aka "single inheritance")
This is not possible in C#:
(Multiple inheritance, use interfaces instead in C#)
Upvotes: 10
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
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:
Upvotes: 10
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
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