|
-
February 5th, 2010, 11:15 AM
#1
real world polymorphism
Hi guys,
Hope u all doing good.Getting some very nice knowledge and concept clearing answers from this forum .Ok so now can anybody tell me taking some real world example and then showing it in code that how polymorphism works and explaining everything that polymorphism can do ..
waiting for the answers 
Thanks
Sid
-
February 5th, 2010, 11:35 AM
#2
Re: real world polymorphism
We could tell you everything about polymorphism and use our time and yours, or you could read everything about polymorphism and use just your time 
MSDN Polymorphism:
http://msdn.microsoft.com/en-us/libr...52(VS.80).aspx
Edit: Ask some specific questions and we can guide you from there.
-
February 5th, 2010, 01:21 PM
#3
Re: real world polymorphism
Take a look at the library itself. There are many examples of polymorphism, one of which are the Stream classes, i.e.,
Code:
using ( Stream s = new FileStream( path" ) )
{
// work with the interface here, it does not matter which type of stream it is
}
-
February 5th, 2010, 01:29 PM
#4
Re: real world polymorphism
 Originally Posted by mariocatch
that's the worst possible explanation BaseClass, DerivedClass, A, B, C, D, no wonder that so few understand the concept.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 5th, 2010, 02:02 PM
#5
Re: real world polymorphism
It's a start. He can read it and ask specific questions instead of, explain and show me how to do this.
-
February 5th, 2010, 02:32 PM
#6
Re: real world polymorphism
mariocatch ...thanks for the link but the thing is that i am very new to c# and it gets kinda hard to read code and directly understand it , maybe you can remember ur first start days when its really hard to figure out what exactly is it's use and meaning and thats why i wrote that please explain with some real world examples as it gets simple to understand if the concept is related to real world things...i appreciate ur help though but is's not easy to understand that msdn code..
Thanks
Sid
-
February 5th, 2010, 02:39 PM
#7
Re: real world polymorphism
and also polymorphism can be demonstrated following the interface way ie no code just some empty classes and showing how polymorphism will work
Thanks
Sid
-
February 5th, 2010, 02:42 PM
#8
Re: real world polymorphism
 Originally Posted by memeloo
that's the worst possible explanation  BaseClass, DerivedClass, A, B, C, D, no wonder that so few understand the concept.
Why not post a comment about what's wrong with the article on msdn?
-
February 5th, 2010, 03:13 PM
#9
Re: real world polymorphism
 Originally Posted by Arjay
Why not post a comment about what's wrong with the article on msdn?
because no one cares?
 Originally Posted by sidhu688
maybe you can remember ur first start days when its really hard to figure out what exactly is it's use and meaning and thats why i wrote that please explain with some real world examples as it gets simple to understand if the concept is related to real world things
I remember, and I'll try to explain it on my favourite example 
I know the word is scary but in reality polimorphism is very easy (I think) (easier that presented on wikipedia which I don't understand at all), it is about how do you interpret (also cast) a class in some context. see here, the "memloo" and "superhero" classes are polymorphic because they can be seen as a "human" and also as a "superhero". you can also derive a class from an interface, then it can be seen as the particular class or any of the inplemented interfaces. its functions and properties are then limited to the class that is currently being seen.
PHP Code:
class Human
{
protected int power = 5;
public int Power { get { return this.power; } }
}
class Superhero : Human
{
protected new int power = 50;
public int HumanPower { get { return base.power; } }
public new int Power { get { return this.power; } }
}
class Memeloo : Superhero
{
protected new int power = 500;
public int SuperheroPower { get { return base.power; } }
public new int Power { get { return this.power; } }
}
class HumanDesigner
{
public void ShowMeWhoIsHuman()
{
Human human = new Human();
Superhero superhero = new Superhero();
Memeloo memeloo = new Memeloo();
// shows who is a human
Console.WriteLine("is human a human? " + human is Human);
Console.WriteLine("is superhero a human? " + superhero is Human);
Console.WriteLine("is memeloo a human? " + memeloo is Human);
// shows who is a superhero
Console.WriteLine("is human a superhero? " + human is Superhero);
Console.WriteLine("is superhero a superhero? " + superhero is Superhero);
Console.WriteLine("is memeloo a superhero? " + memeloo is Superhero);
// shows who is a memeloo
Console.WriteLine("is human a memeloo? " + human is Memeloo);
Console.WriteLine("is superhero a memeloo? " + superhero is Memeloo);
Console.WriteLine("is memeloo a memeloo? " + memeloo is Memeloo);
// shows powers
Console.WriteLine("human's power: " + human.Power);
Console.WriteLine("superhero's power: " + superhero.Power);
Console.WriteLine("memeloo's power: " + memeloo.Power);
// shows inherited powers
Console.WriteLine("memloo's human power: " + ((Human)memeloo).Power);
Console.WriteLine("memloo's superhero power: " + ((Superhero)memeloo).Power);
Console.WriteLine("memeloo's power: " + memeloo.Power);
}
}
Last edited by memeloo; February 5th, 2010 at 03:23 PM.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 5th, 2010, 03:23 PM
#10
Re: real world polymorphism
Id' replace the new's with overrides in your derived classes which would allow you to do this:
Code:
Human human = new Human();
Human superhero = new Superhero();
Human memeloo = new Memeloo()
Looks a little cleaner in my opinion, shows that they are all of Human descent, and properly abstracted out.
-
February 5th, 2010, 03:26 PM
#11
Re: real world polymorphism
 Originally Posted by memeloo
because no one cares?
I care, man. If you post a comment about the msdn entry, there's a good chance that Microsoft might update the entry. If you don't post it, there's zero chance.
-
February 5th, 2010, 03:33 PM
#12
Re: real world polymorphism
 Originally Posted by mariocatch
Id' replace the new's with overrides in your derived classes which would allow you to do this:
Code:
Human human = new Human();
Human superhero = new Superhero();
Human memeloo = new Memeloo()
Looks a little cleaner in my opinion, shows that they are all of Human descent, and properly abstracted out.
this was my first version ;]
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 5th, 2010, 03:56 PM
#13
Re: real world polymorphism
extended example with one interface and with @mariocatch's suggestion
PHP Code:
interface IBaby
{
string WantALollipop();
}
class Human: IBaby
{
protected int power = 5;
public virtual int Power { get { return this.power; } }
public string WantALollipop()
{
return ("Human baby says: strawberry!");
}
}
class Superhero : Human
{
protected new int power = 50;
public int HumanPower { get { return base.power; } }
public override int Power { get { return this.power; } }
public new string WantALollipop()
{
return ("Superhero baby says: nuclear!");
}
}
class Memeloo : Superhero
{
protected new int power = 500;
public int SuperheroPower { get { return base.power; } }
public override int Power { get { return this.power; } }
public new string WantALollipop()
{
return ("Memeloo baby says: I don't like lollipops!");
}
}
class HumanDesigner
{
public static void ShowMeWhoIsHuman()
{
Human human = new Human();
Human superhero = new Superhero();
Human memeloo = new Memeloo();
// shows who is a human
Console.WriteLine("is human a human? " + (human is Human));
Console.WriteLine("is superhero a human? " + (superhero is Human));
Console.WriteLine("is memeloo a human? " + (memeloo is Human));
// shows who is a IBaby
Console.WriteLine("is human an IBaby? " + (human is IBaby));
Console.WriteLine("is superhero an IBaby? " + (superhero is IBaby));
Console.WriteLine("is memeloo an IBaby? " + (memeloo is IBaby));
// shows who is a superhero
Console.WriteLine("is human a superhero? " + (human is Superhero));
Console.WriteLine("is superhero a superhero? " + (superhero is Superhero));
Console.WriteLine("is memeloo a superhero? " + (memeloo is Superhero));
// shows who is a memeloo
Console.WriteLine("is human a memeloo? " + (human is Memeloo));
Console.WriteLine("is superhero a memeloo? " + (superhero is Memeloo));
Console.WriteLine("is memeloo a memeloo? " + (memeloo is Memeloo));
// shows powers
Console.WriteLine("human's power: " + human.Power);
Console.WriteLine("superhero's power: " + superhero.Power);
Console.WriteLine("memeloo's power: " + memeloo.Power);
// shows inherited powers
Console.WriteLine("memloo's human power: " + ((Human)memeloo).Power);
Console.WriteLine("memloo's superhero power: " + ((Human)memeloo).Power);
Console.WriteLine("memeloo's power: " + memeloo.Power);
// shows what babys of each incarnation want
Console.WriteLine("human's baby: " + human.WantALollipop());
Console.WriteLine("superhero's baby: " + superhero.WantALollipop());
Console.WriteLine("memeloo's baby: " + memeloo.WantALollipop());
}
}
Last edited by memeloo; February 5th, 2010 at 04:21 PM.
Reason: replaced new with override
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 5th, 2010, 04:06 PM
#14
Re: real world polymorphism
Need to use override instead of new in your derived classes.
Since all of your Human instances of of type Human directly, they will all be calling Human.WantALollipop() in your last writelines.
If you use override, you don't have to worry about casting explicitly to derived types since it'll use the overridden implementation rather than the 'new' hidden implementation.
-
February 5th, 2010, 04:06 PM
#15
Re: real world polymorphism
ok so here is the program parts and the questions
protected new int power = 50; why the new keyword is used here ?
public int HumanPower { get { return base.power; } } // will this return the power of the base class?
public new int Power { get { return this.power; } } // is this used for returning power of the current object ?
Console.WriteLine("is human a human? " + human is Human);
Everything is coming false when i am running this code and a green line is coming below these and says the given expression is never of the provide ('oops_Concepts.Human) type ?
Console.WriteLine("memloo's human power: " + ((Human)memeloo).Power);
((Human).memeloo) what does this mean ?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|