×

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# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 minutes read.

Dictionary in C#

C #: We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework. C # is related to other widely...

3 minutes read.

C# Namespace

C# Provides Concept of namespacing to organize your classes in a good manner. It makes the application easy to handle. It is like packaging of java. The System is a namespace...

1 minute read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 minutes read.

C# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute read.

C# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 minutes 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# 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# 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# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 minutes read.

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

1 minute read.

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# 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# 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.

Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications. We know that the...

3 minutes read.

C# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

4 minutes read.

C# Keywords

Keywords are the reserved words with special meaning. They can’t be used as variables and Identifiers. If you want to use them as identifiers, you must use ‘@’ character prior...

1 minute read.