Inheritance in C#

Problem
You want to know about object inheritance in C# and its key advantages.

Solution
Inheritance is a process where one type acquires the members of another type. The class that’s inherited from is the base or parent class in C#. The inheriting class is the child class. You can add more members to the child class and override their existing members. One advantage of inheritance is code reusability. Another is the removal of duplicate code.

Discussion
Consider the automobile and truck. A truck is an automobile, but it’s a sub-type of automobile. Not all automobiles have beds for hauling like trucks do. You could model this relationship in OOP and show inheritance. For example, the parent class Automobile would contain methods such as Drive() and Stop() as well as properties such as Color and Make. The child class Truck would inherit the methods and properties of the parent class Automobile but would contain more members such as BedLength.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s