inheritance in c#

By | February 11, 2019

Inheritance in c#-: Inheritance is a “is a kind of” relationship and the cornerstone of object-oriented programming. For example, a rectangle, an ellipse, and a triangle are “a kind of” shape.

Without inheritance, we should have to fully and independently implement the rectangle, ellipse, and triangle classes. It is better to extrapolate the common elements of any shape and place those elements into a general class.

That general class then can be inherited into the specific shape classes. This avoids having to implement the same code in every shape class. Code reuse is one of the primary benefits of inheritance. The general class is called a base class. The specific classes, such as rectangle, ellipse, and triangle, are considered derived classes. The base class contains the common members of the related types, which are inherited by a derived class.

Inheritance provides hierarchical clarity. The Employee class and the derived types form a hierarchy. A real-world example is the .NET Framework Class Library (FCL), which contains hundreds of classes. The FCL is organized along namespaces and inheritance. Without inheritance, it would be harder to navigate the FCL. A great example is the SocketException class. In this Image shows the hierarchy of the SocketException class. It is helpful to know that all exception classes start with System.Exception.

 

example of inheritance in c#

example inheritance c#

Cross-language inheritance-: It assumes that both the base and derived classes adhere to the Common Language Specification (CLS). If not, there is no guarantee that cross-language inheritance will work. For example, the following class, although perfectly okay in C#, is incompatible with Visual Basic .NET. Visual Basic .NET is case-insensitive, making Method A in the following code ambiguous:

public class XBase {
public void MethodA() {
                                            }
public void methoda() {
                                         }
                                      }

example of cross level inheritance -:

cross level inheritance

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *