Polymorhism
Polymorhism
of different types to be treated as objects of a common base type. It enables a single method, operator, or object
to behave in different ways based on the context. Polymorphism means "many forms", and it occurs when we
have many classes that are related to each other by inheritance.
Inheritance lets us inherit fields and methods from another class and Polymorphism uses those methods to perform
different tasks. This allows us to perform a single action in different ways.
Types of Polymorphism in C#
Achieved using method overloading or operator overloading. Compile-time polymorphism, also called static
polymorphism, is implemented using method overloading. It allows a class to have multiple methods with the
same name but different method signatures (different method’s parameter lists).
1. Method Name: This is the identifier you use to call the method (e.g., CalculateArea, GetName,
ProcessData).
2. Parameter List: This includes the number of parameters, The data type of each parameter: What kind of
information each parameter represents (e.g., int, string, bool, a custom class).
3. Order of the parameters: The sequence in which the parameters must be provided when method is called.
4. Parameter modifiers: Keywords like ref, out, params that affect how parameters are passed to the method.
Note: The return type of a method (e.g., int, string, void) is not part of the method signature in C#. You cannot
have two methods with the same name and the exact same parameter list, even if they have different return
types. The compiler wouldn't be able to tell which one you're trying to call.
Runtime polymorphism allows objects to be treated as instances of their base class. Runtime polymorphism, or
dynamic polymorphism, is achieved by method overriding. Method overriding occurs when a subclass/derived
class/child class provides a different implementation for a method that is already defined in the superclass or base
class. The method in the subclass must have the same signature as the method in the base class. By overriding a
method, the subclass can modify the behaviour of the inherited method. It allows a derived class to provide a
specific implementation of a method that is already defined in its base class.
Key Points:
Virtual Method:
Override Keyword:
The derived class method must use the override keyword to provide a new implementation.
// Base class
class Animal
{
// Virtual method to allow overriding
public virtual void MakeSound()
{
MessageBox.Show("Animal makes a sound");
}
}
// Derived class
class Dog : Animal
{
// Overriding the base class method
public override void MakeSound()
{
MessageBox.Show("Dog barks");
}
}
// Derived class
class Cat : Animal
{
// Overriding the base class method
public override void MakeSound()
{
MessageBox.Show("Cat meows");
}
}
class Program
{
static void Main(string[] args)
{
Animal myAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();
myAnimal.MakeSound();
myDog.MakeSound();
myCat.MakeSound();
}
}
This code demonstrates polymorphism in C# using inheritance and method overriding.
1. Base Class (Animal):
• Contains a virtual method MakeSound() that can be overridden by derived classes.
• Default implementation myAnimal.MakeSound(); outputs: "Animal makes a sound".
2. Derived Classes (Dog and Cat):
▪ Both inherit from Animal.
▪ Override the MakeSound() method to provide specific behavior:
• Dog outputs: "Dog barks".
• Cat outputs: "Cat meows".
Animal a: This part declares a variable named a. The type of this variable is Animal. Think of it as creating a
container that is designed to hold objects of type Animal.
= new Dog(): This part creates and assigns a new object of the Dog class to the variable a of class Animal. Dog
is a class that inherits from the Animal class. So, a Dog is a type of Animal.
Creates a new Dog object in memory. This object will have all the properties and behaviors defined in the Dog
class, and it will also inherit the properties and behaviors from the Animal class. Assigns a reference to this newly
created Dog object to the variable a. The key takeaway here is the declared type of the variable a is Animal. The
actual runtime type of the object being referenced by a is Dog. Because Dog is a type of Animal (due to inheritance
as class Dog : Animal { }), you can treat a Dog object as an Animal object. This allows to:
Method overriding is possible only in derived classes. A method is overridden in the derived class when it is
inherited from the base class.
Both the overridden method and the virtual method must have the same access level modifier