Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Object being the base class

Tags:

c#

inheritance

This question may be usual for many, i tried for an hour to understand the things but getting no proper explanation.

MSDN says, System.Object is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy.

When C# doesn't allow multiple inheritance, how can I inherit, say a Class A to Class B. ? Because all classes, already inherits from System.Object right? Here I am talking about the normal inheritance.

Class A { --- }
Class B : A { --- }

Please, clear my doubts. Thank you.

Update:

Again, My doubt is about, All classes inheriting from System.Object, then that would make Class B having Class A as well as System.Object. From my above example

like image 817
Nagaraj Tantri Avatar asked Nov 25 '11 11:11

Nagaraj Tantri


People also ask

Can object be a base class?

No it will not create an objects of the base class, the inherited class's objects can have accessibility to the base class properties(according to the protection level). so that the particular members(available to the inherited class) are only initialized, no object of base class is created.

What does System object mean?

A System object™ is a specialized MATLAB® object. Many toolboxes include System objects. System objects are designed specifically for implementing and simulating dynamic systems with inputs that change over time. Many signal processing, communications, and controls systems are dynamic.

What is the base class of an object in C#?

A base class is the immediate “parent” of a derived class. A derived class can be the base to further derived classes, creating an inheritance “tree” or hierarchy. A root class is the topmost class in an inheritance hierarchy. In C#, the root class is Object .


3 Answers

Correct, C# only allows single inheritence. The System.Object class is inherited implicitly by your Class A. So Class B is-a A, which is-a System.Object. This is taken care of by the compiler so you don't need to explicitly say that Class A : System.Object (though you can if you want).

like image 96
Mark Avatar answered Nov 07 '22 07:11

Mark


Very easy. Ape inherits from animal, chimpanzee inherits from ape. Chimpanzee inherits from animal too, but not primarily, only through ape. In .NET, if class does not state its inheritance explicitly, the compiler adds IL code to inherit it from System.Object. If it does, it inherits System.Object through parent types.

like image 45
idm Avatar answered Nov 07 '22 08:11

idm


Look, you can only have one father. But your father also can have a father. Thus, you inherit some attributes from your grandfather. Dog class inherits from Mammals, which in turn inherits from Animal class, which in turn inherits from LivingThing class.

like image 5
Saeed Neamati Avatar answered Nov 07 '22 08:11

Saeed Neamati