Polymorphism Concept OOPS

Hello everyone! Today, we are going to explore an essential concept in Object-Oriented Programming (OOP) - Polymorphism. Specifically, we'll focus on three of its main forms: method overloading, method overriding, and method hiding. Understanding these concepts is crucial for any programmer working with OOP languages, such as C#.

Polymorphism

Polymorphism, derived from Greek words meaning "many forms," is a feature of OOP that allows objects of different types to be treated as objects of a parent type. It includes method overloading, method overriding, and method hiding.

Method Overloading

Method overloading is a feature that allows a class to have multiple methods with the same name but different parameters. It increases the readability of the program and is a way to achieve compile-time polymorphism.

Method Overriding

Method overriding is a feature that allows a subclass to provide a specific implementation of a method already provided by its parent class. It is a way to achieve runtime polymorphism. The method must be marked with the virtual keyword in the parent class and with the override keyword in the child class.

Method Hiding

Method hiding is a feature where a method in a subclass hides a method in a parent class. It is similar to method overriding but is used when you don't want the subclass to inherit the parent class's method. This is done using the new keyword.

Comparison
 

Method Overloading Method Overriding Method Hiding
Achieves compile-time polymorphism Achieves runtime polymorphism Hides a method in a parent class
Increases code readability Promotes code reusability and flexibility Promotes encapsulation
Can make code complex if overused Can lead to confusion if not used carefully Can lead to unexpected behavior if not used carefully


Real-life Example in C#

using System;
public class HelloWorld
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Try Polymorphism in C#");
        Base b = new Child();
        b.Print();
        b.Print("Red");
        b.PrintAdd(1, 2);
        b.PrintAdd(1f, 4f);
    }
    public class Base
    {
        public void PrintAdd(int a, int b)
        {
            Console.WriteLine("Base:Add {0}", a + b);
        }
        public void PrintAdd(float a, float b)
        {
            Console.WriteLine("Base:Add {0}", a + b);
        }
        public virtual void Print(string color)
        {
            Console.WriteLine("Base:Print {0}", color);
        }
        public virtual void Print()
        {
            Console.WriteLine("Base:Print");
        }
    }
    public class Child : Base
    {
        public new void Print(string color)
        {
            Console.WriteLine("Child:Print {0}", color);
        }
        public override void Print()
        {
            Console.WriteLine("Child:Print");
        }
    }
}
// Output
/*
Try Polymorphism in C#
Child:Print
Base:Print Red
Base:Add 3
Base:Add 5
=== Code Execution Successful ===
*/

Explanation

In your Base class, you have overloaded the PrintAdd method. One version of the method accepts two integers and the other accepts two floats. This is an example of method overloading, where the same method name is used with different parameters.

In your Child class, you have overridden the Print method with no parameters. This is an example of method overriding, where a subclass provides a different implementation of a method that is already provided by its parent class.

Also in your Child class, you have hidden the Print method that accepts a string parameter. This is an example of method hiding, where a method in a subclass hides a method in a parent class.

When you create a Child object but declare it as a Base object (Base b = new Child();), and call the Print methods, it will call the Child class's Print method with no parameters (due to method overriding) and the Base class's Print method with a string parameter (due to method hiding). The PrintAdd methods will always call the Base class's methods, as these are not overridden or hidden in the Child class.

Conclusion

method overloading, overriding, and hiding are key concepts in C# and Object-Oriented Programming. They enhance code readability, reusability, and flexibility. However, they should be used judiciously to avoid complexity and maintainability issues. Understanding these concepts is crucial for effective programming in C#.


Similar Articles