[go: up one dir, main page]

0% found this document useful (0 votes)
12 views5 pages

Polymorhism

Polymorphism in C# is a fundamental concept of OOP that allows objects of different types to be treated as instances of a common base type, enabling methods to behave differently based on context. There are two main types of polymorphism: compile-time (static) achieved through method overloading, and run-time (dynamic) achieved through method overriding. The document provides examples of both types, illustrating how inheritance and method signatures play a crucial role in implementing polymorphism.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Polymorhism

Polymorphism in C# is a fundamental concept of OOP that allows objects of different types to be treated as instances of a common base type, enabling methods to behave differently based on context. There are two main types of polymorphism: compile-time (static) achieved through method overloading, and run-time (dynamic) achieved through method overriding. The document provides examples of both types, illustrating how inheritance and method signatures play a crucial role in implementing polymorphism.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

In C#, polymorphism is one of the core concepts of object-oriented programming (OOP), and it allows objects

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#

There are two main types of polymorphism:

Compile-time Polymorphism (Static Polymorphism)

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).

It can be differentiated, based on:

• Changing the number of Parameters


• Changing data types of the parameters.
• Changing the Order of the parameters.

Example Method Overloading:

public class Calculator


{
public int Add(int a, double b)
{ a + b; }
public double Add(double a, int b)
{ a + b; }
public double Add(double a, int b, int c)
{ a + b + c; }

} // overloaded method as data type and sequence of data type and


number of parameters in Add methods are different.
* Method Signature is the unique identifier of a method. It tells the C# compiler everything it needs to know to
distinguish one method from another. Parts of Method signature are

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.

Run-time Polymorphism (Dynamic Polymorphism)

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:

The base class method must be declared as virtual to allow overriding.

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".

Consider the part Animal myDog = new Dog();

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.

The line Animal myDog = new Dog(); does the following:

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:

• Store different derived types in a collection of the base type:


Animal[] animals = { new dog, new cat, new bird }; example, you can have an arraythat holds various
types of animals (dogs, cats, birds) because they all inherit from the Animal base class.
• Pass derived type objects as arguments to methods that expect the base type: You can write a method
that takes an Animal as a parameter, and you can pass a Dog object (or a Cat object, or any other Animal
derived type) to that method. The method can then interact with the object through the Animal interface
(the members defined in the Animal class).
• Take advantage of virtual methods: If the Animal class has a virtual method (like MakeSound() in our
example), when you call that method through the a variable, the actual implementation in the Dog class
will be executed at runtime.
• In essence, Animal a = new Dog(); allows you to work with a Dog object through a more general Animal
class. This promotes code flexibility, reusability, and the ability to handle different related types.
Important Points:

Method overriding is possible only in derived classes. A method is overridden in the derived class when it is
inherited from the base class.

A non-virtual or static method cannot be overridden.

Both the overridden method and the virtual method must have the same access level modifier

You might also like