Polymorphism in C# deals with multiple classes having the same name, related to each other. This is also one of the important concepts in object-oriented programming after encapsulation and inheritance.
Polymorphism in C#
Understanding Polymorphism in C#
Polymorphism (or polymorphism) is a term derived from Greek, translated into Vietnamese meaning 'one name, many different forms'.
In other words, an object can have multiple forms or a name with multiple different functions. 'Poly' means many and 'morph' means forms. Polymorphism provides the ability to execute multiple classes with the same name.
As mentioned earlier, polymorphism in C# deals with multiple classes having the same name, related to each other. This is an important concept in object-oriented programming following the concepts of encapsulation and inheritance.
2. Types of Polymorphism in C#
Basically, there are 2 types of polymorphism in C#, including:
- Static Polymorphism / Compile Time.
- Dynamic Polymorphism / Runtime.
3. Static Polymorphism in C# (or Compile Time)
Static polymorphism (or Compile Time) in C# is also known as Early Binding. Overloading is a classic example of static polymorphism.
In overloading, methods/functions have the same name but different signatures. It's also called polymorphism at compile time because the decision of which method to call is made at compile time. Overloading is a concept where methods with the same name differ in parameter sets.
Here, the compiler checks the number and type of parameters passed, then decides which method to call. If no method is found, it reports an error.
- Example of static polymorphism in C#
In the example below, the class includes 2 methods with the same name Add, but different input parameters (the first method has 3 parameters and the second method has 2 parameters).
4. Dynamic polymorphism in C# (or Runtime Polymorphism)
Dynamic polymorphism or Runtime Polymorphism is also known as Late Binding. In which the method name and method signature (number and type of parameters must be the same, but there can be different implementations). Overriding is a classic example of dynamic polymorphism.
Overriding method can be achieved using inheritance. With overriding method, base class and derived class can have the same method name and some differences. The compiler does not recognize the overridden function, so it won't return an error during compilation. The compiler will decide which method to call at runtime and if no method is found, it will report an error.
The compiler demands the Area() method and compiles successfully, but the version of Area() is not determined at compile time but at runtime. Finally, overriding methods must have the same name and signature (number of parameters and types), as virtual or abstract methods are defined in the base class and overridden in the derived class.
A method or function of the base class is available to the derived class (subclass) without using the 'overriding' keyword. The compiler hides the functions or methods of the base class. This concept is also called shadowing or method hiding.
5. Preventing derived classes from overriding virtual members
Virtual members do not depend on how many classes are between virtual members and the originally declared class.
For instance, if class X has a virtual method 'A' and class Y is derived from X, and X is derived from Y, class Z inherits the virtual method 'A' and overrides it.
A derived class can stop virtual inheritance by declaring an overriding member as 'sealed'.
6. Accessing base class virtual members
Using the 'base' keyword, the derived class can access the method.
7. Conclusion
- Polymorphism means a name with multiple forms, different shapes.
- There are 2 different types of polymorphism:
+ Static polymorphism or compile time (e.g., method overloading and operator overloading).
+ Dynamic polymorphism or runtime polymorphism (e.g., overriding).
- Overriding method is different from shadowing.
- Use the 'new' keyword to hide base class members.
- We can prevent derived classes from overriding virtual members.
- It's possible to access a base class virtual member from a derived class.
This article by Mytour just introduced you to the concept of polymorphism in C#. If you have any doubts or questions needing clarification, you can leave your comments below the article. Additionally, readers can refer to some other articles already available on Mytour to learn more about encapsulation in C#.