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.
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
}
Re: real world polymorphism
Quote:
Originally Posted by
mariocatch
that's the worst possible explanation :thumbd: BaseClass, DerivedClass, A, B, C, D, no wonder that so few understand the concept.
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.
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
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
Re: real world polymorphism
Quote:
Originally Posted by
memeloo
that's the worst possible explanation :thumbd: 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?
Re: real world polymorphism
Quote:
Originally Posted by
Arjay
Why not post a comment about what's wrong with the article on msdn?
because no one cares?
Quote:
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 :D
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);
}
}
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.
Re: real world polymorphism
Quote:
Originally Posted by
memeloo
because no one cares?
I care, man. :cool: 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.
Re: real world polymorphism
Quote:
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 ;]
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());
}
}
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.
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 ?