Inheritance

What is inheritance ?
Inheritance in C# is the idea of designing a class that reuses the implementation of an existing class. The primary purpose of inheritance is to reuse code from an existing class. Inheritance allows you to create a new class that starts off by including all data and implementation details of the base class. You can then extend the derived class, to add data or behavior.

Why do you want to reuse code ?
You want to reuse code for the same reasons you make use if design patterns, interfaces, and encapsulation – it reduces uncertainty. Reducing uncertainty means better code – faster, more reliable, more robust and less error-prone applications.

Why does code-reuse reduce uncertainty ?
The code has already been written, tested and possibly released into production. Bugs have already been identified and corrected.
Your developers are not re-inventing the wheel – and possibly doing it worse.
Your developers have more time to concentrate on new functionality – more time to test, refine, improve.

Inheritance Example
You’re developing a trading application. You need to display trade and commission data. You design models for each of these entities. Every model in the application needs to have an Id property to identify the row in the database; a CurrentUser value to identify the last person to update the record and a WorkStationId property to identify where the update was made. Since each model needs to have these properties, they can be abstracted into a base class which will serve as the base for all models. Via inheritance, you ensure each model will have an Id, CurrentUser and WorkStationId values – required for proper operation.

inheritance1

inheritance2inheritance3

Inheritance Example 2
Each time you create a new windows form control you are enabling code-reuse via inheritance.
inheritance4

Consider all the behavior that you get without having to write any code – resizing, moving, drag and drop, maximize and minimize functions, events and container logic.

Posted in Uncategorized

Leave a comment

Design a site like this with WordPress.com
Get started