C# - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C# Polymorphism

C# Polymorphism

shape Description

In object oriented programming, Polymorphism is a programming language ability to represent object in different forms and redefine methods in derived class from base class. C# Polymorphism is a Greek word which means having "many forms". It is considered as one of strong pillar of object oriented programming.

shape Conceptual figure

Virtual Keyword

shape Description

Virtual method allows declare a method in base class that can be redefined in each derived class. It is a method whose behavior can be overridden in derived class.

Sealed Keyword

shape Description

Sealed keyword is used to stop method overriding in a derived classes. Base classes may define and implement virtual methods and derived classes can override them, which means they provide their own definition and implementation.

Compile time Polymorphism

shape Description

Method overloading is achieved by this type of polymorphism. It is also called as Static polymorphism. Here compiler checks the data type and number of parameters passed on to the method and decides which method to call at compile time .Compiler gives an error if there are no methods that match the method signature of the method that is called at compile time. To implement Compile time polymorphism below are the two techniques:
  1. Operator Overloading
  2. Function Overloading

shape Example

[csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SPLessons { class Program { static void Main(string[] args) { ClassX obj = new ClassX(); int c = obj.NumbersAdd(10, 10); int d = obj.NumbersAdd(10, 10, 20); Console.WriteLine(c); Console.WriteLine(d); Console.ReadLine(); } public class ClassX { public int NumbersAdd(int a, int b) { return a + b; } public int NumbersAdd(int a, int b, int c) { return a + b + c; } } } } [/csharp] Output:

Runtime Polymorphism

shape Description

Method overriding is achieved by this type of polymorphism.  At run time it will be decided which method to be called and if no method found it gives an error .
  • Runtime polymorphism is implemented by abstract classes and virtual functions.

shape Example

[csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SPLessons { class Program { static void Main(string[] args) { System.Collections.Generic.List<Shape> shapes = new System.Collections.Generic.List<Shape>(); shapes.Add(new Rectangle()); shapes.Add(new Circle()); foreach (Shape s in shapes) { s.Draw(); } Console.WriteLine("Press any key to exit"); Console.ReadKey(); } public class Shape { public int X { get; private set; } public int Y { get; private set; } public int Height { get; set; } public int Width { get; set; } // Virtual method public virtual void Draw() { Console.WriteLine("Base class draw Method"); } } // Extends base class shape and override draw method of base circle... class Circle : Shape { public override void Draw() { Console.WriteLine("Child class - circle"); base.Draw(); } } // Extends base class shape and override draw method of base circle... class Rectangle : Shape { public override void Draw() { // Code to draw a rectangle... Console.WriteLine("Child class - rectangle"); base.Draw(); } } } } [/csharp] Output: