C# Polymorphism - c# - c# tutorial - c# net




What is polymorphism in C# ?

  • The term "Polymorphism" is the combination of "poly" + "morphs" which means many forms.
  • learn c# tutorials - polymorphism in c#

    learn c# tutorial - polymorphism in c# Example

  • It is a greek word. In object-oriented programming, we use 3 main concepts:
    • inheritance,
    • encapsulation and
    • polymorphism.
  • There are two types of polymorphism in C#:
    • compile time polymorphism and
    • runtime polymorphism.
  • Compile time polymorphism is achieved by method overloading and operator overloading in C#.
  • It is also known as static binding or early binding. Runtime polymorphism in achieved by method overriding which is also known as dynamic binding or late binding.
Polymorphism

Polymorphism

C# Runtime Polymorphism Example

  • Let's see a simple example of runtime polymorphism in C#.
using System;  
public class Animal{  
public virtual void eat(){  
Console.WriteLine("eating...");  
}  
}  
public class Dog: Animal  
{  
public override void eat()  
{  
Console.WriteLine("eating bread...");  
}  

}  
public class TestPolymorphism  
{  
public static void Main()  
{  
Animal a= new Dog();  
a.eat();  
}  
}  
click below button to copy the code. By - c# tutorial - team

C# examples - Output :

eating bread...

C# Runtime Polymorphism Example 2

  • Let's see a another example of runtime polymorphism in C# where we are having two derived classes.
using System;  
public class Shape{  
public virtual void draw(){  
Console.WriteLine("drawing...");  
}  
}  
public class Rectangle: Shape  
{  
public override void draw()  
{  
Console.WriteLine("drawing rectangle...");  
}  

}  
public class Circle : Shape  
{  
public override void draw()  
{  
Console.WriteLine("drawing circle...");  
}  

}  
public class TestPolymorphism  
{  
public static void Main()  
{  
Shape s;  
s = new Shape();  
s.draw();  
s = new Rectangle();  
s.draw();  
s = new Circle();  
s.draw();  

}  
}  
click below button to copy the code. By - c# tutorial - team

C# examples - Output :

drawing...
drawing rectangle...
drawing circle...
Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

Runtime Polymorphism with Data Members

  • Runtime Polymorphism can't be achieved by data members in C#. Let's see an example where we are accessing the field by reference variable which refers to the instance of derived class.
using System;  
public class Animal{  
public string color = "white";  

}  
public class Dog: Animal  
{  
public string color = "black";  
}  
public class TestSealed  
{  
public static void Main()  
{  
Animal d = new Dog();  
Console.WriteLine(d.color);  

}  
}  
click below button to copy the code. By - c# tutorial - team

C# examples - Output :

white


Related Searches to C# Polymorphism

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×