Reputation: 5232
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
Upvotes: 9
Views: 8878
Reputation: 3221
Seems like you have a slight confusion with the meaning of 'multiple inheritance'?
Multiple inheritance is not when 'B inherits from A, and A inherits from O'. That's just a simple inheritance hierarchy -- which is a feature of C++, Java and C#.
In the above case, we'd say that B inherits directly from A, and inherits indirectly from O. B inherits the (non-private) members from A, and, indirectly, the (non-private) members of O.
C++ in addition supports true multiple inheritance, which can sometimes be called 'mix-in inheritance': An example of multiple inheritance would be
class A : public O {};
class B : A, O {};
Here B inherits directly from O, and also inherits directly from A -- In this case two copies of O's members exist in B, and to access the members of B that come from O, you need to specify which of those copies you want:
e.g. b.O::omember;
or b.A::omember;
With large C++ class frameworks, you can often get undesired copies of base classes in your derived classes when you use multiple inheritance. To get round this, C++ provides virtual inheritance, which forces only one copy of the virtual base class to be inherited: The following example should make this clear (or make it even more confusing!)
// Note, a struct in C++ is simply a class with everything public
struct O { int omember; };
struct A1 : O {};
struct B1 : O, A1 {};
struct A2 : virtual O {};
struct B2 : virtual O, A2 {};
B1 b1;
B2 b2;
// There are two 'omember's in b1
b1.omember; // Compiler error - ambiguous
b1.A1::omember = 1;
b1.O::omember = 2;
// There's only one 'omember' in b2, so all the following three lines
// all set the same member
b2.A2::omember = 1;
b2.O::omember = 2;
b2.omember = 3;
Upvotes: 1
Reputation: 35822
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.
Upvotes: 5
Reputation: 1748
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.
Upvotes: 7
Reputation: 77
Multiple inheritance means inheriting from multiple classes into one class. Your example is valid, but you wouldn't be able to do the following:
Class B : A,C { --- }
A definition for multiple inheritance:
"Multiple inheritance is a feature of some object-oriented computer programming languages in which a class can inherit behaviors and features from more than one superclass."
Upvotes: 0
Reputation: 5894
You can't inherit two classes, but you can inherit a hierarchy of classes.
System.Object
|
\-- Class A {}
|
\-- Class B {}
public class ClassA
{
}
public class ClassB : ClassA
{
}
public class ClassC : ClassB
{
}
Upvotes: 0
Reputation: 12458
Multiple inheritance means that e.g. class A inherits from both class B and class C directly, like this:
class A : B, C
That doesn't work in C#. Your example means that you inherit from a class which itself inherits from another, like this:
class A : object
{
}
class B : A
{
}
That is possible in C#.
Upvotes: 1
Reputation: 44181
Class A
inherits from System.Object. Class B
inherits from class A
, which inherits from System.Object
Upvotes: 0
Reputation: 1924
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).
Upvotes: 17