C# Polymorphism

HTML
CSS
Bootstrap
JavaScript
C#
SQL
Salesforce Admin
Exercise
Study Material

Polymorphism

Polymorphism, a term derived from Greek meaning “many forms,” is a fundamental concept in object-oriented programming (OOP) that allows objects of different classes to respond to the same method call in potentially varied ways. Imagine having a toolbox with different tools, each designed for a specific task. Polymorphism lets you treat all the tools similarly (picking them up, using them), but their functionality differs based on the actual tool you use.

Why Use Polymorphism?

Polymorphism brings several advantages to your C# programs:

– Flexibility and Code Reusability: By focusing on behaviors instead of concrete implementations, you can write code that works with various objects as long as they adhere to a specific interface or inherit from a base class. This promotes code reusability and simplifies handling different object types.

– Maintainability: Changes made to a base class or interface can propagate to derived classes that implement them, potentially reducing the need to modify code in multiple places.

– Elegant Design: Polymorphism promotes cleaner and more expressive code, often leading to well-designed and adaptable applications.

Key Aspects of Polymorphism in C#:

There are two main ways polymorphism manifests in C#:

1. Compile-Time Polymorphism (Method Overloading):

Method Overloading: This allows you to define multiple methods with the same name but different parameter lists within the same class. The compiler determines the correct method to call based on the arguments provided at compile time.

C#
public class Calculator
{
    public int Add(int a, int b) { return a + b; }
    public double Add(double a, double b) { return a + b; }
}

2. Runtime Polymorphism (Method Overriding):

Method Overriding: This occurs when a derived class inherits a method from a base class and provides its own implementation. The decision of which method to call (base class or derived class) is made at runtime based on the object’s actual type.

C#
public class Animal
{
    public virtual void MakeSound() { Console.WriteLine(“Generic animal sound”); }
}

public class Dog : Animal
{
    public override void MakeSound() { Console.WriteLine(“Woof!”); }

Here, the MakeSound() method is declared as virtual in the Animal base class, allowing derived classes like Dog to override it with their specific sound.

Common Pitfalls and Best Practices:

– Clarity and Naming: Use descriptive method names and parameter lists to avoid confusion when using polymorphism.

– Designing Base Classes: Carefully consider the functionalities a base class should provide and which methods should be declared as virtual for potential overriding.

Beyond the Basics:

– Abstract Classes and Interfaces: These are essential tools for promoting polymorphism. Abstract classes define a blueprint with virtual methods, while interfaces specify functionalities without implementation. Derived classes can inherit from abstract classes and implement interfaces, leveraging polymorphism effectively.

– Late Binding: The process of determining the actual method to call at runtime is known as late binding. This is a key aspect of runtime polymorphism.

Calling to Action:

Get hands-on with polymorphism! Here are some ideas:

– Create a base class Shape with a virtual Draw() method. Implement this method in derived classes like Square and Circle to showcase polymorphism.

– Explore how polymorphism can be used with interfaces to achieve a high degree of flexibility and loose coupling in your code.

– Experiment with method overloading to create methods with different functionalities based on the provided arguments.

Practice

1. Create a base class Shape with a virtual method Draw().Define two derived classes: Square and Circle, inheriting from Shape.Override the Draw() method in Square to display “Drawing a square”.Override the Draw() method in Circle to display “Drawing a circle”.In the Main() method, create an array of type Shape and initialize it with instances of Square and Circle.Iterate through the array and call the Draw() method on each element to demonstrate polymorphism

OUTPUT 

2. Define a base class device with a virtual method shop() that prints “going to purchase mobile” as a default action.Implement two derived classes:mobile: Inherit from device and override the shop() method to print “we are going mobile shop to purchase a phone”.laptop: Inherit from device and override the shop() method to print “we are going laptop shop to purchase a laptop”.In the Main() method:Create instances of mobile and laptop using the base class device.Call the shop() method on each instance to demonstrate polymorphic behavior, showing the specific purchase action for each device type.

OUTPUT