You are on page 1of 4

Dynamic polymorphism

Dynamic polymorphism is process or mechanism


in which a call to an overridden method is to
resolve at runtime rathen than compiler time
Dynamic polymorphism is a process or
mechanism in which a call to an overridden
method is to resolve at runtime rather than
compile-time. It is also known as runtime
polymorphism or dynamic method dispatch. ... In
this process, an overridden method is called
How can we override a base class method under derived class?
To override the base method into a derived class, the first base class method should be declared with a ‘virtual’ keyword. Then only the base class methods can override the parent method which is declared using the ‘virtual’ keyword.
public class Car { public virtual void Speed() { Console.WriteLine("Speed is 60kmh"); } } public class Audi : Car { public override void Speed() { Console.WriteLine("Speed is 120kmh"); } } When we declare the ‘virtual’ method in the base class, then the base class declares that the method can be implemented based on derived class implementation.
Points to remember
An only virtual or abstract method can be overridden
Even if the base class method declared as virtual then also derived class may or may not override the method.
How to execute the base class method if it is overridden in the derived class?
If we want to call the base class method using derived class instance then to call the base class method, we can use ‘base’ keyword to call the base class method.
public class Car { public virtual void Speed() { Console.WriteLine("Speed is 60kmh"); } } public class Audi : Car { public override void Speed() { base.Speed(); //it will call parent class Speed() method } } Also, we can call base class method from derived class at any time.
How to access a hidden base class method in the derived class?
We can access the hidden base class method from derived class instance by casting the instance of the derived class to the base class.
For example in the below code snippet we have ‘Car’ class as the base class and ‘Audi’ class as a derived class, so we have cast ‘Audi’ class instance to ‘Car’ class to access the base class hidden method
class Program { static void Main() { Audi car = new Audi(); ((Car)car).Speed(); //car.Speed(); Console.ReadLine(); } } public class Car { public virtual void Speed() { Console.WriteLine("Speed is 60kmh"); } } public class Audi : Car { public new void Speed() { Console.WriteLine("Speed is 120kmh"); } }What is output of below program?
class Program { static void Main() { Car car = new Audi(); car.Speed(); Console.ReadLine(); } } public class Car { public virtual void Speed() { Console.WriteLine("Speed is 60kmh"); } } public class Audi : Car { public new void Speed() { Console.WriteLine("Speed is 120kmh"); } } Output of the above code snippet
Speed is 60kmhAs ‘Audi’ class is derived from ‘Car’ class, and derived class speed() method is hidden, However, we create an instance of the derived class as the method is hidden it will execute the base class method.
What is the output of below program? What is the importance of new keyword?
class Program { static void Main() { Car car = new Audi(); car.Speed(); Console.ReadLine(); } } public class Car { public virtual void Speed() { Console.WriteLine("Speed is 60kmh"); } public virtual void EnginePower() { Console.WriteLine("70kW"); } } Output of the above code snippet
Speed is 60kmh 70kWIn both cases, base class methods will be executed as the derived call method has been hidden. if base class and derived class both contain methods with the same name and if we use a base reference with child object then base class methods will be always called.
The above program gets executed with a warning message. As the compiler can see the method is hidden but the compiler is unable to understand whether the hidden method is intended or not.
To specify the hidden intention of a method we use ‘new’ keyword in C#, it specifies that method hiding is international.
Why we need to overload methods?
Sometimes we need to execute the same logic should be executed with a different signature. For example, to add two integers, two string, and two doubles we should define three methods with the same name as shown in the below code snippet.
public class Addition { public int Add(int a, int b) { return a + b; } public string Add(string a, string b) { return a + b; } public double Add(double a, double b) { return a + b; } } Also, sometimes we need to execute some of the class with different values so we an overloaded constructor too.
Read more about Polymorphism and Online Free Polymorphism Multiple Choice Question Quiz for Interview preparations. also, you can check Inheritance interview questions and read more about abstraction, encapsulation for interview preparation.
Add comment if you have any Polymorphism Interview Questions.
MAHESHAUGUST 5, 2020C#, CSHARP, INTERVIEW QUESTIONS, OOPS, POLYMORPHISMLeave a Reply
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
Post navigation
PREVIOUSPrevious post:Polymorphism
NEXTNext post:Interface
Block Puzzle Android Game – Smart Brain
Block Puzzle Game 2021
Quick links
- Quote Generator
- Programming Daily News
- Latest Tech Invention News
- Write an Article - Guest Post
- Ask a question
Search
Search for:
Recent posts
Getting Started with Angular Using Visual Studio Code
What is 401 Unauthorized Error? How to fix it?
Programming
Why Backlinks Are Required For A Website?
Newborn Photography – Tips for Getting Great Newborn Pictures
My Amazon Picks
Dickie's Men's Heavyweight Crew Neck Short Sleeve Tee …
$14.99
(16501)
GUESS Black Ionic Plated Genuine Diamond Dial Bracel…
$119.93
(308)
Ads by Amazon
Top Questions
Ultimate Member – Default pages not created on activation asked by Himanshu S
How to decrypt md5 password in c#.net? asked by Anynoumus
How to create RAR file in c#.net? asked by Pooja Patel
Create SOAP envelope in
CLASS PROGRAM { STATIC VOID MAIN() {
CAR CAR = NEW AUDI();
CAR.SPEED(); CONSOLE.READLINE(); } } PUBLIC CLASS CAR { PUBLIC VIRTUAL VOID SPEED()
{ CONSOLE.WRITELINE("SPEED IS 60KMH"); } } PUBLIC CLASS AUDI : CAR { PUBLIC NEW VOID SPEED()
{
CONSOLE.WRITELINE("SPEED IS 120KMH");
{
Method overloading is an example of static polymorphism, while method
overriding is an example of dynamic polymorphism. An important example
of polymorphism is how a parent class refers to a child class object. ... Here,
Cat satisfies the IS-A relationship for its own type as well as its super class
Animal

You might also like