×

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance, encapsulation and polymorphism. There are two types of polymorphism named as compile time polymorphism and run time polymorphism. Compile time polymorphism is achieved by method overloading and operator overloading. Runtime polymorphism is achieved by method overriding. Dynamic Method Dispatch: it is used to achieve runtime polymorphism. In this, Call to an    overridden method is resolved at runtime. Let's see an example of runtime polymorphism by dynamic method dispatch. At compile time the compiler checks for the existence of the method in the base class. If the method is derived at runtime then the derived method is called at runtime else the base class method is called. 
using System;
public class Shape
{
            public virtual void draw()
            {
                        Console.WriteLine("drwaing shape...");
            }
}
public class Triangle:Shape
{
            public override void draw()
            {
                        Console.WriteLine("draawing triangle...");
            }
}
public class RuntimePolymorphism
{
            public static void Main(string[] args)
            {
                        Shape s=new Triangle();
                        s.draw();
            }
}
Output
draawing triangle...
C# Polymorphism: with multilevel inheritance
using System;
public class Shape
{
            public virtual void draw()
            {
                        Console.WriteLine("drwaing shape...");
            }
}
public class Triangle:Shape
{
            public override void draw()
            {
                        Console.WriteLine("draawing triangle...");
            }
}
public class RightTriangle:Triangle
{
            public override void draw()
            {
                        Console.WriteLine("drawing RightTriangle");
            }
}
public class RuntimePolymorphism
{
            public static void Main(string[] args)
            {
                        Shape s=new Triangle();
                        s.draw();
                        Triangle t=new RightTriangle();
                        t.draw();
            }
}
Output
draawing triangle...
drawing RightTriangle

Related Topics

C# static field

In C#, static filed is the one which belongs to the class not instance hence we don’t need to create instance to access static field. The memory is assigned only...

5 minutes read.

C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 minutes read.

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 minutes read.

C# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

3 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# Destructor

Destructors works in the opposite way. They basically destruct the objects. We can’t send parameter in a destructor. We also can’t apply any modifier to the destructor. Likewise constructor, destructor...

1 minute read.

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 minutes read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.

C# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

C# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private ...

10 minutes read.

C# Structs

In C#, Structs are like classes but they are used to create light weight objects like color, Rectangle, etc. Structs are value type unlike classes that are reference type. Using...

3 minutes read.

C# do While loop

C# do while loop is responsible for executing a part of the code several number of times. It is an exit controlled loop which is recommended to use if the...

3 minutes read.

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.