Function Overloading
We can have many functions with the same name and different implementations in the same scope. It is just that the definition of the functions must differ from each other based on the type of arguments or the number of arguments passed. We can not overload a function based on the return type of the function.
Let us see an example where we will overload the print() function that will print different data type variables.
CODE
using System;
namespace PolymorphismExample {
class Printdata {
void print(int i) {
Console.WriteLine("Printing int: {0}", i );
}
void print(double f) {
Console.WriteLine("Printing float: {0}" , f);
}
void print(string s) {
Console.WriteLine("Printing string: {0}", s);
}
static void Main(string[] args) {
Printdata p = new Printdata();
// Call print to print integer
p.print(5);
// Call print to print float
p.print(500.263);
// Call print to print string
p.print("Hello Java");
Console.ReadKey();
}
}
}
Output
Printing int: 5
Printing float: 500.263
Printing string: Hello Java
Dynamic Polymorphism
In C#, we can create abstract classes that provide a partial implementation of the class. We can just define the methods in the abstract class without its implementation. These methods are known as abstract methods. Whenever a class derives the abstract class, it needs to implement the abstract methods.
Important points of Abstract Class
- We can create an object/instance of an abstract class.
- We can only declare the abstract method inside the abstract class.
- We can declare the abstract classes to be sealed.
Code
using System;
namespace PolymorphismApplication {
abstract class Shape {
public abstract int area();
}
class Rectangle: Shape {
private int length;
private int width;
public Rectangle( int a = 0, int b = 0) {
length = a;
width = b;
}
public override int area () {
Console.WriteLine("Rectangle class area :");
return (width * length);
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle r = new Rectangle(10, 7);
double a = r.area();
Console.WriteLine("Area: {0}",a);
Console.ReadKey();
}
}
}
Output
Rectangle class area :
Area: 70
We can also achieve Dynamic Polymorphism by inheritance. If we want some functions defined in the base class to be implemented again in the derived classes, then we can use virtual functions.
For example:-
Let us say, we have a vehicle class. It has a method//function named numberOfTires(It returns the number of tires of the vehicle).
The derived classes of the vehicle class can be a car, bus, bike, etc. Each of these derived classes has a different number of tires. Hence, they would implement their own numberOfTires function in their way.
Code
using System;
class Vehicle // Base class (parent)
{
public virtual void numberOfTires()
{
Console.WriteLine("The vehicle has 8 tires");
}
}
class car : Vehicle // Derived class (child)
{
public override void numberOfTires()
{
Console.WriteLine("The car has 4 tires");
}
}
class bus : Vehicle // Derived class (child)
{
public override void numberOfTires()
{
Console.WriteLine("The bus has 8 tires");
}
}
class bike : Vehicle // Derived class (child)
{
public override void numberOfTires()
{
Console.WriteLine("The bike has 2 tires");
}
}
class Program
{
static void Main(string[] args)
{
Vehicle myVehicle = new Vehicle(); // Create a Animal object
Vehicle myBike = new bike(); // Create a Pig object
Vehicle myBus = new bus(); // Create a Dog object
myVehicle.numberOfTires();
myBike.numberOfTires();
myBus.numberOfTires();
}
}
Output:
The vehicle has 8 tires
The bike has 2 tires
The bus has 8 tires
FAQs
-
What is Polymorphism?
Polymorphism refers to “having many forms”. This concept of Object-Oriented Paradigm explains the situation where something occurs in many different forms.
-
Why are different types of Polymorphism?
Polymorphism can be of two types:
- Static Polymorphism: In this polymorphism, the information for calling the function is collected at the compile time.
- Dynamic Polymorphism: In this polymorphism, the call to an overridden method is resolved at the run time rather than compile time.
Key Takeaways
In this article, we have extensively discussed the following things:
- We first discussed what is Polymorphism.
-
Then we discussed the different types of Polymorphism.
We hope that this blog has helped you enhance your knowledge regarding Polymorphism in the GO language and if you would like to learn more, check out our articles here.
Check out this article - Compile Time Polymorphism
Recommended Readings:
Do upvote our blog to help other ninjas grow. Happy Coding!
