user3197065
user3197065

Reputation: 21

C# types derived from system.object

As I understand, C# support Multiple inheritance using Interfaces indeirectly. When I was going through CLR via C# book, I got a query on this. The book says

The runtime requires every type to ultimately be derived from the System.Object type. This means that the following two type definitions are identical:

// Implicitly derived from Object
class Employee {
....
}

// Explicitly derived from Object
class Employee : System.Object {
...
}

If this is the correct statement, can the code mentioned below be true?

// Implicitly derived from Object
class SoftwareEngineer : Employee 
{
....
}

// Explicitly derived from Object
class SoftwareEngineer : Employee, System.Object {
...
}

Upvotes: 2

Views: 297

Answers (7)

Kjartan
Kjartan

Reputation: 19091

This, if permitted

class SoftwareEngineer : Employee, System.Object { ... }

Would essentially be the same as:

class SoftwareEngineer : Employee { ... }

..since an Employee is implicitly, an Object anyway (but you cant use that form anyway).

You could do something like the following with interfaces:

// Implements IEmployee, and an IPerson (interfaces), 
// while inheriting from System.Object:
class SoftwareEngineer : System.Object, IEmployee, IPerson { ... }

..but the System.Object here would still be superfluous, since again - SoftwareEngineer would always implicitly be an Object anyway.

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

At first, C# *doesn't support multiple inheritanc*e, so

  class SoftwareEngineer : Employee, System.Object { ... } // <- Compile time error

The phrase that "every type to ultimately be derived from the System.Object type" means that every heritage chain should end at Object, e.g.

  SafeHandleMinusOneIsInvalid // <- inherited from 
  SafeHandle                  // <- which in turn inherited from
  CriticalFinalizerObject     // <- which finally inherited from
  Object                      // <- The ultimate base class

Interfaces are not classes; they are kind of contracts, e.g

  public class MyVersion: 
    IComparable<MyVersion>,  // <- I guarantee, there's "int CompareTo(MyVersion other)" method
    ICloneable,              // <- I guarantee, there's "Object Clone()" method
  ...

Upvotes: 1

Rik
Rik

Reputation: 29243

The runtime requires every type to ultimately be derived from the System.Object type

In your second example, SoftwareEngineer inherits from Employee, which in turn, inherits from Object The second declaration is illegal because C# does not allow multiple inheritance (implementing multiple interfaces is a different concept), but it in effect its still inheriting from Object because it's base type does.

Upvotes: 1

Thomas Weller
Thomas Weller

Reputation: 11717

It's a frequent misconception, but interface implementation is completely different from inheritance. Other than they look similar they have nothing in common.

Interface implementation happens at compile-time and basically says "The implementing class must contain certain member signatures". Inheritance on the contrary is dynamic runtime behavior which is realized using a Virtual Method Table.

So implementing multiple interfaces is no problem, while multiple inheritance is forbidden.

Upvotes: 4

fhnaseer
fhnaseer

Reputation: 7277

No.

Employee class derives from System.Object. So indirectly SoftwareEngineer derives from System.Object.

// Explicitly derived from Object
class SoftwareEngineer : Employee, System.Object {
...
}

This will give you syntax error. Even if there is some other class it is not possible. The hierarchy is one class will derive from father, and father will driver from grand father and so on.

Upvotes: 1

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60493

You can't inherit from more than one class in c#.

So second is false.

Employee inherits from Object SoftwareEngineer inherits from Employee

But SoftwareEngineer can access visible methods from Engineer AND visible methods from Object (like ToString for example)

Upvotes: 1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

You cannot do this, because multiple inheritance from classes is not allowed:

class SoftwareEngineer : Employee, System.Object

Line above will give you compilation error:

'SoftwareEngineer' cannot have multiple base classes: 'Employee' and 'System.Object'

But thus Employee implicitly inherits from System.Object that means SoftwareEngineer also will be inherited from System.Object (you can think of it as 'vertical' inheritance):

class Employee : System.Object

class SoftwareEngineer : Employee

Upvotes: 6

Related Questions