This chapter discussed basic inheritance concepts and how these are implemented in C#. The following are some of the important points that were covered.
Aggregation implements has a relationship, inheritance implements is a relationship.
Class hierarchies are formed after the same principles as those used by taxonomists. They move from the topmost general categories to the lowermost specialized categories.
Through inheritance, class members in general classes can be reused in specialized classes. Without inheritance, we have two options for creating objects that have unique as well as common class members. Write one class from which all the individual objects are instantiated, or write one class for each distinguishable type of objects. Both approaches have serious drawbacks. Inheritance takes the best from both approaches while leaving their problems behind.
A derived class extends its base class by adding new class members while inheriting the class members of the base class. The derived class can modify the inherited base class members as required. A virtual function member of a base class can be modified in the derived class by overriding it with the use of the override keyword.
A derived class is also called a parent class, and a base class is called a child class. A base class of a base class, and so on, is called an ancestor class. A derived class of a derived class, and so on, is called a descendant class.
It is important to show whether your intent is to override or re-implement a function member with the override and new keywords.
A class cannot be derived from a sealed class.
A protected class member can only be accessed from within its own class and any descendant class.
Even though a derived class inherits the private class members of its base class, it cannot access those class members directly from within its own class definition.
The internal protected access modifier provides internal access or protected access.
A derived class never inherits the constructors of its base class, but they can be called with a constructor initializer from the derived class's constructors. The compiler automatically attaches constructor initializers (that call the default base class constructor) to any constructor in a derived class, which do not contain an explicitly specified constructor initializer.
An indexer of a base class can be called with the following syntax
Class members in a base class can be called from the derived class with the keyword base. This is referred to as base access.
Inheritance is necessary for reusing large parts of the .NET Frameworks class library.
Method overriding and method overloading are different mechanisms. An overriding method in a derived class must have the same signature, return type, and access modifier as the method it is overriding in the base class. In contrast, an overloading method has the same name but a different set of formal parameter types than the method it is overloading.