Polymorhism in C#
Polymorphism (meaning "many forms") is a fundamental concept in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common
1. Compile-Time Polymorphism (Static Polymorphism/Overloading):
This type of polymorphism is resolved at compile time. It's achieved through method overloading and operator overloading.
-
Method Overloading: Defining multiple methods within the same class with the same name but different parameter lists (different number, types, or order of parameters). The compiler determines which method to call based on the arguments provided during the method call.
C#public class Calculator { public int Add(int a, int b) { return a + b; } public double Add(double a, double b) { return a + b; } public int Add(int a, int b, int c) { return a + b + c; } } // Usage Calculator calc = new Calculator(); int sum1 = calc.Add(2, 3); // Calls Add(int, int) double sum2 = calc.Add(2.5, 3.7); // Calls Add(double, double) int sum3 = calc.Add(1, 2, 3); // Calls Add(int, int, int) -
Operator Overloading: Defining how standard operators (like +, -, *, /, ==, !=, etc.) should behave when used with objects of a custom class.
C#public struct ComplexNumber { public double Real; public double Imaginary; public static ComplexNumber operator +(ComplexNumber c1, ComplexNumber c2) { return new ComplexNumber { Real = c1.Real + c2.Real, Imaginary = c1.Imaginary + c2.Imaginary }; } } // Usage ComplexNumber num1 = new ComplexNumber { Real = 2, Imaginary = 3 }; ComplexNumber num2 = new ComplexNumber { Real = 1, Imaginary = 2 }; ComplexNumber sum = num1 + num2; // Uses the overloaded + operator
2. Run-Time Polymorphism (Dynamic Polymorphism/Overriding):
This type of polymorphism is resolved at run time. It's achieved through inheritance and virtual methods.
-
Method Overriding: A derived class provides a specific implementation for a method that is already defined in its base class. The base class method must be declared as
virtual,abstract, oroverride.virtual: The base class method has an implementation, but derived classes can override it.abstract: The base class method has no implementation (it's a declaration only), and derived classes must override it. This makes the base class abstract.override: Used in the derived class to indicate that it is overriding a virtual or abstract method from the base class.
C#public class Animal { public virtual void MakeSound() { Console.WriteLine("Generic animal sound"); } } public class Dog : Animal { public override void MakeSound() { Console.WriteLine("Woof!"); } } public class Cat : Animal { public override void MakeSound() { Console.WriteLine("Meow!"); } } // Usage Animal animal1 = new Animal(); Animal animal2 = new Dog(); Animal animal3 = new Cat(); animal1.MakeSound(); // Output: Generic animal sound animal2.MakeSound(); // Output: Woof! animal3.MakeSound(); // Output: Meow!In this example, even though
animal2andanimal3are declared asAnimal, the correctMakeSound()method is called at run time based on the actual object type (Dog or Cat). This is the power of run-time polymorphism.
Key Benefits of Polymorphism:
- Code Reusability: You can write generic code that works with objects of different classes.
- Extensibility: It's easy to add new classes and have them work with existing code without modification.
- Maintainability: Changes to one part of the code are less likely to affect other parts.
- Flexibility: You can treat objects in a more abstract way, making your code more adaptable to different situations.
Polymorphism is a crucial concept for writing robust, flexible, and maintainable object-oriented code in C#. Understanding the difference between compile-time and run-time polymorphism is essential for effective use of this powerful feature.
Comments
Post a Comment