Introduction
Real-world Example
A scientific calculator is an extended form of a calculator. Here, the calculator is the parent, and the scientific calculator is the child object. The child object inherits the key properties of the base object. For example, the calculator has common functions such as adding, subtracting, multiplying, and dividing. But the scientific calculator has some advanced functions such as power and others.
Programming Example
The process of acquiring the existing functionality of the parent and the newly added features and functionality of a child object.
What is Inheritance
Inheritance is one of the three foundational principles of Object-Oriented Programming (OOP) because it allows the creation of hierarchical classifications. Using inheritance, you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it.
In the language of C#, an inherited class is called a base class. The class that does the inheriting is called the derived class. Therefore a derived class is a specialized version of a base class. It inherits all of the variables, methods, properties, and indexers defined by the base class and adds its unique elements.
Inheritance Example
Diagram
The following diagram shows the inheritance of a shape class. Here the base class is Shape, and the other classes are its child classes. In the following program, we will explain the inheritance of the Triangle class from the Shape class.