# C Sharp Programming/Inheritance
## Explanation By Analogy

What is the benefit of inheritance?
* It saves you a lot of typing
* 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:
* Fly
* Breed
* 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:
* 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.
* The derived class cannot change the definition of an inherited member.
* Constructors and destructors are not inherited. All other members of the base class are inherited.
* The accessibility of a member in the derived class depends upon its declared accessibility in the base class.
* A derived class can override an inherited member.
An example of inheritance:

## Subtyping Inheritance

The code sample below shows two classes, Employee and Executive. Employee has the methods GetPayCheck and Work.
We want the Executive class to have the same methods, but differently implemented and one extra method, AdministerEmployee.
Below is the creation of the first class to be derived from.
Now, we create an Executive class that will override the GetPayCheck method:
You'll notice that there is no Work method in the Executive class, as it is inherited from Employee.

## 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 base 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 virtual.
To override a method, use the override keyword:
A missing new or override keyword for a derived method may result in errors or warnings during compilation.: Here an example:
The Square class method Area will result in a compilation error, if it is derived from the ShapesA class:
The same method will result in a compilation warning, if derived from the normal Shapes class:

## References

* ↑ Greg Beech (2010-03-09). "C# design: Why is new/override required on abstract methods but not on virtual methods? / Answer". http://efreedom.com/: eFreedom. Retrieved 2011-08-11. Using either the C# 3.0 compiler as shipped in .NET 3.5 SP1, or the C# 4.0 compiler as shipped in .NET 4.0, I get the following error for your first example: [...] And the following warning for the second one: [...] In the first case it's an error because you aren't actually overriding the base method, which means there is no implementation for the abstract method in a concrete class. In the second case it's a warning because the code is technically correct, but the compiler suspects that it isn't what you meant. This is one of the reasons it's generally a good idea to enable the "treat warnings as errors" compilation setting. {{cite web}}: External link in |location= (help)