advertisement
Polymorphism and Method Overriding in C#
Polymorphism
Polymorphism is derived from two Greek words, namely Poly and Morphos. Poly means many 2nd Morphos means forms. Polymorphism means existing in multiple forms. Polymorphism is the ability of an entity to behave differently in different situations. Consider the following two methods in a class having the same name but different signatures performing the same basic operation but in different ways:
Area (float radius)
Area (float base, float height)
The two methods calculate the area of the circle and triangle taking different parameters and using different formulae. This is an example of polymorphism in C#.
Polymorphism allows methods to function differently based on the parameters and their data types.
Implementation
You can implement polymorphism in C# through method overloading and method overriding. You create multiple methods with the same name in a class or in different classes having different method bodies or different signatures. Methods having the same name but different signatures in a class are referred to as overloaded methods. Here, the same method performs the same function on different values.
Methods inherited from the base class in the derived class and modified within the derived class elt referred to as overridden methods. Here, only the body of the method changes to Tunct according to the required output.
Method Overriding:-
class Hardware
{
public virtual bool Turnon() {return true;}
}
class Monitor: Hardware{
{
public override bool Turnon() {return true;}
}
Method Overloading:-
class Hardware
{
public bool Turnon() {return true;}
public bool Turnon(string ans) {return true;}
}
Compile-time and Run-time Polymorphism
Polymorphism can be broadly classified into two categories, compile-time polymorphism and run-polymorphism. differentiates between compile-time and run-time polymorphisms.
Compile-time Polymorphism:-
- Is implemented through method overloading.
- Is executed at the compile-time since the compiler knows which method to execute depending on the number of parameters and their data types.
- Is referred to as static polymorphism.
Run-time Polymorphism:-
- Is implemented through method overriding.
- Is executed at run-time since the compiler does not know the method to be executed, whether it is the base class method that will be called or the derived class method.
- Is referred to as dynamic polymorphism.
//Example 1: Compile-Time Polymorphism
class calculator
{
// methods to add two int
public int add(int a, int b)
{
return a + b;
}
// method to add three integers
public int add(int a, int b, int c)
{
return a + b + c;
}
}
class program
{
static void Main()
{
calculator calobj = new calculator();
// Using the first add method
int sum2num = calobj.add(10, 5);
Console.WriteLine($"Sum of two numbers : {sum2num}");
//using the second add method
int sum3num = calobj.add(10, 20, 30);
Console.WriteLine($"Sum of three numbers: {sum3num}");
Console.ReadKey();
}
}
//Example 2: Runtime Polymorphism
class shape
{
public virtual void draw()
{
Console.WriteLine("Drawing a shape");
}
}
class circle: shape
{
public override void draw()
{
Console.WriteLine("Drawing a circle");
}
}
class square: shape
{
public override void draw()
{
Console.WriteLine("Drawing a Square");
}
}
class program
{
static void Main()
{
shape shape1 = new circle();
shape shape2 = new square();
shape1.draw(); //Output: Draw a circle
shape2.draw(); //Output: Draw a square
Console.ReadKey();
}
}
Method Overriding
Method overriding is a feature that allows the derived class to override or redefine the methods of the base class. Overriding a method in the derived class can change the body of the method that was declared in the base class. Thus, the same method with the same name and signature declared in the base class can be reused in the derived class to define a new behavior. This is how reusability is ensured while inheriting classes.
virtual and override Keywords
You can override a base class method in the derived class using appropriate C# keywords such as virtual and override. If you want to override a particular method of the base class in the derived class, you must declare the method in the base class using the virtual keyword. A method declared using the virtual keyword is referred to as a virtual method.
In the derived class, you must declare the inherited virtual method using the override keyword This is mandatory for any virtual method that is inherited in the derived class. The override keyword overrides the base class method in the derived class.
Calling the Base Class Method
Method overriding allows the derived class to redefine the methods of the base class. Redefining the base dass methods allows you to access the new method but not the original base class method. Sometimes, you might want both, the base class method as well as the derived class method, to be executed. In this case, you can create an instance of the base class, which allows you to access the base class method, and an instance of the derived class, to access the derived class method.
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0