Runtime polymorphism in c#

No.of Views1532
Bookmarked6 times
Downloads 
Votes0
By  kirtan007   On  19 Jun 2010 21:06:47
Tag : OOP , Miscellaneous
In this article I will write the code first about the classes and explain you later how its achieved in c#.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

What is runtime polymorphism?

By runtime polymorphism we can point to any derived class from the object of the base class at runtime that shows the ability of runtime binding .In Object oriented languages like Visual C++ we can use pointers to achieve runtime polymorphism C# is also object oriented programming language that facilitates the same functionality of runtime polymorphism as VC++ does.

Implementation

class Shape
    {public virtual void Draw()
        {

        }
    }class Ractangel:Shape
    {public override void Draw()
        {
            Console.WriteLine("Rectangle Drawn ");
        }
 
    }class Circle:Shape
    {public override void  Draw()
        {
            Console.WriteLine("Circle Drawn ");
            
        }
    }class Traingle:Shape
    {public override void Draw()
        {
            Console.WriteLine("Triangle Drawn ");
            
        }
    }static void Main(string[] args)
        {/* Testing Polymorphism */Shape[] s = new Shape[3];/* Polymorphic Objects *//* creating Array with Different types of Objects */s[0] = new Circle();
            s[1] = new Ractangel();
            s[2] = new Traingle();
 
      Console.WriteLine("\n\nRuntime polymorphism test\n\n");for (int i = 0; i < 3; i++)
            {
                s[i].Draw();
            }
            Console.ReadKey();
        }
Image Loading

Explanation of the code

Here we have created one base class called shape. We have inherited the shape class in to three derived classes called Rectangle, Circle and triangle. each contain method called Draw() .to achieve runtime polymorphism we need to declare the method as virtual which we want to call from each derived object .Now we want to call method draw of each object from same base class object .

By runtime polymorphism you can see we have built the array which containing different types of objects in same array. And in Finally main Method of the program we Called Draw () function of the different objects which were reside in same array .At runtime. Though we are calling Draw function from the base class object we can call method of Derived classes

Conclusion

We have learnt how to achieve runtime polymorphism in Visual C#.

Sample Project Source

Download source files -24 kb

 
Sign Up to vote for this article
 
About Author
 
kirtan007
Occupation-
Company-
Member Type-Senior
Location-Not Provided
Joined date-02 Jul 2009
Home Page-http://kirtan.uni.cc
Blog Page-
He completed his Bachelor of Computer Application from Gujarat University 2009 .He is doing Master of Computer Application from Gujarat Technological University right now .. His area of Interests are Web Hacking , C# .net Windows form ,asp.net , WPF ,Silverlight ,SQL Server and Some PHP.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top