C Sharp Programming/Inheritance

Explanation By Analogy
What is the benefit of inheritance?


 * 1) It saves you a lot of typing
 * 2) It saves you from repeating yourself.

Inheritance explained by analogy

Suppose you want to create an Eagle, a Falcon and a vulture. In order to create these flying creatures you notice that each of these creatures:
 * 1) Fly
 * 2) Breed
 * 3) Eat

Let us assume for the sake of argument that all three types of birds: fly, breed and eat in exactly the same way.

Without inheritance, you would be forced to copy code. i.e. the same code which causes an eagle to fly would also be copied to make the vulture fly. And it is axiomatic to programmers - who are a lazy bunch, not wanting to repeat themselves - that repetition is almost always a bad thing.

Note the eagle, falcon and vultures are all in fact birds. Accordingly, you could say that a bird, generally speaking, always has the characteristics of eating, breeding and flying. So using "inheritance" you could create generic 'bird' prototype, which eats, breeds and flies, and then once that is defined, you can have all other specific breeds of birds inherit those characteristics. In other words, using the prototype, you can design other specific birds off that prototyped design.

This means that the falcon automatically knows how to fly because it inherits that behaviour from the general Bird class. You basically don't have to repeat yourself.

Inheritance
Inheritance is the ability to create a class from another class, the "parent" class, extending the functionality and state of the parent in the derived, or "child" class. It allows derived classes to overload methods from their parent class.

Inheritance is one of the pillars of object-orientation. It is the mechanism of designing one class from another and is one of the ideas for code reusability, supporting the concept of hierarchical classification. C# programs consist of classes, where new classes can either be created from scratch or by using some or all properties of an existing class.

Another feature related to inheritance and reusability of code is polymorphism, which permits the same method name to be used for different operations on different data types. Thus, C# supports code reusability by both features.

Important characteristics of inheritance include:


 * 1) A derived class extends its base class. That is, it contains the methods and data of its parent class, and it can also contain its own data members and methods.
 * 2) The derived class cannot change the definition of an inherited member.
 * 3) Constructors and destructors are not inherited. All other members of the base class are inherited.
 * 4) The accessibility of a member in the derived class depends upon its declared accessibility in the base class.
 * 5) A derived class can override an inherited member.

An example of inheritance:

Subtyping Inheritance
The code sample below shows two classes, and. has the methods and.

We want the class to have the same methods, but differently implemented and one extra method,.

Below is the creation of the first class to be derived from.

Now, we create an class that will override the  method:

You'll notice that there is no method in the  class, as it is inherited from.

Virtual Methods
If a base class contains a virtual method that it calls elsewhere and a derived class overrides that virtual method, the base class will actually call the derived class' method:

Constructors
A derived class does not automatically inherit the base class' constructors, and it cannot be instantiated unless it provides its own. A derived class must call one of its base class' constructors by using the keyword:

Inheritance keywords
The way C# inherits from another class syntactically is by using the operator.

Example:

To indicate a method that can be overridden, you mark the method with.

To override a method, use the  keyword:

A missing or  keyword for a derived method may result in errors or warnings during compilation.: Here an example:

The class method  will result in a compilation error, if it is derived from the  class:

error CS0534: 'ConsoleApplication3.Square' does not implement inherited abstract member 'ConsoleApplication3.Shapes.Area'

The same method will result in a compilation warning, if derived from the normal class:

warning CS0114: 'ConsoleApplication3.Square.Area' hides inherited member 'ConsoleApplication3.Shapes.Area'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.