Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
| Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old December 7th, 2007, 02:15 PM
Friend of Wrox
 
Join Date: Aug 2006
Location: Bangalore, Karnataka, India.
Posts: 231
Thanks: 0
Thanked 1 Time in 1 Post
Default run time polymorphism

well any body can tell me good example of run time polymorphism.
please.


thanks......
__________________
thanks......
  #2 (permalink)  
Old December 8th, 2007, 07:03 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well anytime you use a subclass and a base class, or an interface you are using polymorphism.

If you use the ADO.Net DbProviderFactories.GetFactory() to load the ADO.Net provider at runtime from the config file thats probably a good example.

/- Sam Judson : Wrox Technical Editor -/
  #3 (permalink)  
Old December 8th, 2007, 09:23 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Consider the following class design:
Code:
class Animal{
    public string Speak(){
        return "Speak";
    }
}
class Cat : Animal{
    public override Speak(){
        return "Meow";
    }
}
class Dog : Animal{
    public override Speak(){
        return "Woof";
    }
}
Now we create a few instances and call the method:
Code:
Animal oTest;
oTest = new Cat();
oTest.Speak(); //returns "Meow"
oTest = new Dog();
oTest.Speak(); //returns "Dog"
In this case we are working with the type "Animal". It's the interface of the superclass that the other types are derived from. We create instances of two different concrete types and call the same method on each, but get a different result. The sub classes provide the implementation to the superclass interface.

Another example is something like the Console.Write() method which has many overloads. You call the method the same for different types. The method is polymorphic in that is handles each type in a different way.


Take a look at the Wikipedia entry for Polymorphism as it relates to OOP. The examples are similar to mine.


-Peter
  #4 (permalink)  
Old December 8th, 2007, 01:59 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Is this really 'runtime' polymorphism - I'd say t was more compile time :)

/- Sam Judson : Wrox Technical Editor -/
  #5 (permalink)  
Old December 8th, 2007, 11:04 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Can we really qualify it as either "compile time" versus "run time". Does the compiler know what implementation is being called? I'm no expert on the under workings of the CLR or the compiler but I would imagine that this qualifies as a runtime example. Perhaps it would be more convincing if we were instantiating the subclass by dynamically loading the type or something else more runtime-ish. But realistically, is this any different than a more explicit, pre-compiled example? Ultimately we are programming against an interface (either an explicit interface or a super class' implied interface) so the concrete implementation is unknown with regards to the method calls.

-Peter
  #6 (permalink)  
Old December 12th, 2007, 08:38 AM
Authorized User
 
Join Date: Sep 2007
Location: , , Finland.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can do some really cool things with reflection like declare new types on-the-fly and such if you want to do it all at runtime :)



Similar Threads
Thread Thread Starter Forum Replies Last Post
run-time error(s) Chacko C++ Programming 0 March 4th, 2007 02:28 PM
Run time errors Martin Outlaw Excel VBA 2 August 23rd, 2006 05:13 AM
Design-Time or Run-Time now ? ALGNET .NET Framework 2.0 1 July 31st, 2006 04:43 AM
Validation at Run Time nicesukhi Visual Studio 2005 0 July 20th, 2006 02:43 AM
Run time Help Dazzer96 Access VBA 2 May 3rd, 2006 07:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.