In some of my most recent articles, we saw what inheritance, polymorphism, and interfaces are and how they are implemented in C#. In this article, we will discuss another important C# concept. We take a look at what extension methods are, how they are used, and how can we implement them in C#.
Interfaces in C#
In a previous article, I explained inheritance in C#. We studied how to implement inheritance between classes. In all of the examples in that article, we performed single inheritance. Single inheritance refers to the idea of inheriting from one class. In C# a child class can only have one parent class. However, a parent class can have many child classes. A child class can have many levels of parent class but it has only one immediate parent. But what if you want a class to inherit from multiple classes? Suppose you have a class “Mechatronics” and you want this class to inherit from both “Mechanics” and ‘Electronics” classes. In C# you CANNOT do so. This is where interfaces come handy. In C# you can implement multiple inheritance via interfaces. In this article, we will study interfaces in detail.
Polymorphism in C#
Polymorphism is one of the three basic components of Object Oriented Programming (OOP). Polymorphism literally means having multiple shapes. In terms of programming, it is referred to as “one interface, multiple functions.” In C#, polymorphism is implemented via method overloading, method overriding and inheritance. We will look at all of these concepts in detail in this article. Polymorphism can be broadly classified into two categories: static polymorphism and dynamic polymorphism. Static polymorphism is implemented via method overloading, and dynamic polymorphism is implemented via inheritance and method overriding.
Inheritance in C#
In one of my previous articles, I introduced you to object-oriented programming (OOP). There are three pillars of OOP which are cumulatively known as PIE: Polymorphism, Inheritance, and Encapsulation. In this article, we will focus on Inheritance and the different ways of implementing inheritance in C#.