Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 7188 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > C# > Run Time PolyMorphism Example---4 For Beginner?????? ...
Sabarimahesh

Run Time PolyMorphism Example---4 For Beginner??????

 Code Snippet posted by: Sabarimahesh | Posted on: 5/8/2012 | Category: C# Codes | Views: 1025 | Status: [Member] | Points: 40 | Alert Moderator   


 class Program

{
static void Main(string[] args)
{
Baseclass bc;
bc = new Baseclass();
bc.Show();
bc = new DerivedClass();
bc.Show();
bc = new DerivedClass2();
bc.Show();
Console.ReadLine();

}

}

class Baseclass
{
public virtual void Show()
{
System.Console.WriteLine("Baseclass::Show");
}
}

class DerivedClass : Baseclass
{
public override void Show()
{
System.Console.WriteLine("DerivedClass::Show");
}
}

class DerivedClass2 : DerivedClass
{
public override void Show()
{
System.Console.WriteLine("DerivedClass2::Show");
}
}



OutPut


Baseclass::Show
DerivedClass::Show
DerivedClass2::Show


The function Show() of Base class Baseclass is declared as virtual, while the implementation of Show() in successive Derived classes is decorated with the modifier override. Next, we succesively create objects of each class and store their reference in base class reference variable Baseclass and invoke Show(). The rite versions of Show get invoked based on the object the reference variable refers to.


Life is a Race
Thanks & Regards
By
Sabari Mahesh P M
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/18/2013 1:57:00 PM