How does inheritance between classes work in C #? [closed]

1

I have class 1 and class 2, class 2 inherits from class 1.

Will other classes inheriting from class 2 inherit the attributes of class 1?

    
asked by anonymous 12.07.2017 / 23:23

1 answer

1

Inheritance is a code reuse mechanism, be it algorithms, or data structure. Well roughly inheritance makes a copy of the class that inherited and sticks to this new one, so everything in class 1 will be in class 2, so if you create a class 3 inheriting from class 2, everything you have in class 2 will be in class 3, including what has already been copied from class 1.

Of course the mechanism is a bit more complex than this, it is not a de facto copy, and there are member protection mechanisms, so private members of class 2 or 1 will not be accessible by class 3, and a protected member in class 1 must remain at least protected in class 2 for class 3 to see.

There is still the issue of virtualization, so class 3 can only override what is allowed by class 2, no matter how it is in class 1, although class 2 can not give more access than it has.

Get the type Object . It is inherited by all kinds of C #, directly, implicitly, or indirectly because the new type inherited from a type that had already inherited from Object . Then Equals(Object) , GetHashCode() , GetType() and ToString() will be available in all types written in .NET (ok, there are exceptions, but it escapes from normal .NET and C # typing).

Note that there are static members in it. These members obviously do not participate in the inheritance, they are out. Protected members can be accessed by the derived type, but are not available for access outside of it, so they are not completely public, they will not be part of the public contract of the derived type, but they are there. The same goes for private members who are not even documented, are present in the object, but can not access them in a normal way.

So it's a hierarchy where members of the ascending type will always be present in their offspring.

    
13.07.2017 / 19:28