What is Polymorphism Overl

What is Polymorphism, Overloading, Overriding in ASP.Net, C#?

Technology Education

What is Polymorphism?
Polymorphism means “Many Forms”. In Polymorphism, poly means “Many” and morph means “Forms”. Polymorphism allows us same perform a task in multiple forms or ways. It is applied to the functions or methods.
Types of Polymorphism are:
— Compile-time polymorphism (Method overloading)
— Run-time polymorphism (Method Overriding)

Method Overloading

Q1: What is Method Overloading?
In method overloading, more than one method shares the same method name with different signature (Parameters) in the class.
— Return type may or may not be be same, but we must have to change the parameter,
— Compiler automatically calls required method to check number of parameters and their type which are passed into that method.
— Method Overloading is a Compile time polymorphism.
— Also Called, Early Binding or static binding

Q2: Why Method Overloading?
Whenever we want to do same task with different parameter list ,we will go for method overloading.

Q3: How Method Overloading can be achieved?
— By changing the number of parameters used.
— By changing the order of parameters.
— By using different data types for the parameters.

Polymorphism Overloading

Method Overriding

Q1: What is Method Overriding?
When a function of base or parent class is re-defined in the derived class, is called as Overriding
Overriding is all about giving a specific implementation to the inherited method of parent class.
— Method Overriding is a Run time/dynamic polymorphism.
— Overriding is a feature that is available only while using Inheritance.
— Both Actual and derived methods must have same name and signatures [same number of arguments and same type of arguments].
— Also called, late binding or
— Which version of a method is executed will be determined by the object that is used to invoke it.
If an object of a parent class is used to invoke the method, then the version in the parent class will be executed,
but if an object of the subclass is used to invoke the method, then the version in the child class will be executed.

Q2: Why Method Overriding?
It is used when a class that extends from another class wants to use most of the feature of the parent class but still
wants to implement specific or own or different functionality in certain cases.

Q3: How Method Overloading can be achieved?
— By override Parent function in the derived class.
— By using same name, same number of arguments and same type of arguments, but different functionality in derived class.
— By using inheritance and using virtual & override.

Polymorphism Overloading0

Overloading Vs Overriding
— Binding:
Overloading happens at compile-time while Overriding happens at runtime:
The binding of overloaded method call to its definition has happens at compile-time, however binding of overridden method
call to its definition happens at runtime.
— Scope:
Overloading is being done in the same class while for overriding base and child classes are required.
— Performance:
Overloading gives better performance compared to overriding. Reason: The binding of overridden methods is being done at runtime.
— Argument list should be different while doing method overloading. Argument list should be same in method Overriding.
— Method overloading doesn’t need inheritance Mehod overriding needs inheritance
— Access modifier can be any in Overloading, where as Access modifier must be public in overriding.

Polymorphism Vs Inheritance
— Polymorphism give ability to define same task in multiple forms or ways, where as Inheritance is one in which a new class is
created (derived class) that inherits the features from the already existing class(Base class).
— Polymorphism applied to functions or methods, where as Inheritance applied to classes.
— Polymorphism allows the object to decide which form of the function to implement at compile-time (overloading) as well as
run-time (overriding).
Inheritance supports the concept of reusability and reduces code length in object-oriented programming.

Leave a Reply

Your email address will not be published. Required fields are marked *