Inheritance in .NET
A class inherits the members of its direct base class. Inheritance means that a class implicitly contains all members of its direct base classexcept for the instance constructors, distructors and static constructors of the base class. Some important aspects of inheritance are:
- Inheritance is transitive: If C is derived from B, and B is derived from A, then C inherits the members declared in B as well as members declared in A
- A derived class extends it s direct base class. A derived class can add new members to those it inherits, but it can not remove the definition of an inherited member.
- Instance constructors, destructors and static constructors are not inherited. But all other members are, regardless of their declared accessibility. However depending on their declared accessibility, inherited members might not be accessible in a derived class.
- A derived class can hide inherited members by declaring new members with the same name or signature. However, hiding an inherited member does not remove that member - it merely makes that member inaccessible directly through the derived class.
- An instance of a class contains a set of all instance fields declared in the class and its base class, and an implicit conversion exists from a derived class type to any of its base class type. Thus reference to an instance of some derived class can be treated as areference to an instance of any of its base classes.
- A class can declare virtual methods, properties and indexers, and derived classes can override the implementation of these function members. This enables classes to exhibit polymorphic behavior wherein the actions performed by a function member invocation varies depending on the runtime type of the instance through which that function member if invoked.
- Programming languages such as Java and the .NET Framework languages do not allow multiple inheritance. Multiple inheritance can be emulated in .NET using Multiple Interface Inheritance. C++ allows multiple inheritance.