Polymorphism

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Polymorphism

Post by hurstshifter »

Had my first taste of polymorphism in C++ the other day in my summer session class. I've got to admit that I'm thrilled at its possibilities and am really glad (if only for learning inheritance and polymophism) that I took this class.

That is all. Discuss amongst yourselves. I'll give you a topic: the chickpea is neither a chick, nor a pea.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Polymorphism

Post by dandymcgee »

Image

Polymorphism and inheritance are both cool concepts, but I still haven't figured out when it's appropriate to use them.
I've found my habits often evolve much more around composition, and (rarely) aggregation.

This is probably a bad thing, and in the future I hope to learn to successfully apply these methods in tandem. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Apoc
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Fri Jul 31, 2009 5:52 am

Re: Polymorphism

Post by Apoc »

dandymcgee wrote:Image

Polymorphism and inheritance are both cool concepts, but I still haven't figured out when it's appropriate to use them.
I've found my habits often evolve much more around composition, and (rarely) aggregation.

This is probably a bad thing, and in the future I hope to learn to successfully apply these methods in tandem. ;)
Inheritance and polymorphism are two of the main ingredients in object oriented programming. If you're not using them; you might as well just use plain old C.

(Excuse the C# code here, my C++ is a bit rusty)

Imagine you had a class, for example class Car.

Code: Select all

public class Car
{
public string Name;

public virtual void Buy() 
{
    FindOnEbay();
}
}
Car is a very broad class. You could then inherit from Car, and create new classes. Eg; Ford, GM, Honda, Toyota, etc. You've now made more specific 'Car's. (Yes, I know, they're the manufacturers, keep reading.)

Code: Select all

public class Ford : Car
{
public string Model;
public int HorsePower;

public override void Buy()
{
    BuyFromDealership(Name, Model);
}
}
Ford now has all the fields that Car has (inheritance), plus Model and HorsePower. It also changes how Buy() works (polymorphism). Obviously; this is a pretty basic explanation, but you should get the point. Ford 'is a' Car. So you can cast a Ford to a Car, and vice versa. (Careful; as the Car might not actually be a Ford!)
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Polymorphism

Post by avansc »

this is exactly why there is a influx of sub par programmers. they think that using every feature of language,
or implementing every know design pattern or design methodology makes them great programmers, or have
the attitude that if someone doesn't they are sub par programmers or should stick to a "other" language...
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Polymorphism

Post by hurstshifter »

avansc wrote:this is exactly why there is a influx of sub par programmers. they think that using every feature of language,
or implementing every know design pattern or design methodology makes them great programmers, or have
the attitude that if someone doesn't they are sub par programmers or should stick to a "other" language...
Not sure who this was being directed at but I don't think anyone in this thread has expressed the need to use every feature of the language nor has anyone claimed that they are better programmers for knowing polymorphism. As for me, I literally just learned this concept and it was interesting for me as I am very new to OO design. I think that people looking down on other programmers who over-utilize OO design techniques is just as counter-productive as the OO-abusers themselves. While it is definitely important to know when and where to use each feature of the language, it's also important to know how to use these features. I think we can agree on that.


EDIT: "If you're not using them; you might as well just use plain old C." - Sorry, I noticed this comment afterwards. This I do not agree with.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
Apoc
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Fri Jul 31, 2009 5:52 am

Re: Polymorphism

Post by Apoc »

avansc wrote:this is exactly why there is a influx of sub par programmers. they think that using every feature of language,
or implementing every know design pattern or design methodology makes them great programmers, or have
the attitude that if someone doesn't they are sub par programmers or should stick to a "other" language...
You seem to have quite a high-horse attitude. By no means did I state: 'You're not using OOP to it's fullest. Go use C'. I stated a fact, and an opinion. Then gave an example of how to use something he isn't quite familiar with. (Assuming so at least)

Using every feature of the language, or every known design pattern/methodology, usually means your code is over-complicated (especially when you consider C++).

Inheritance and polymorphism is what makes OOP so useful.

Also, I'm not sure how you have the knowledge, or ability, to call someone a sub-par programmer. Especially when you have no idea who I am, or what I've done in the past. Sure, I could be a newcomer to programming. Then again, I could be Bjarne. How would you know? Take your righteous attitude and give it to someone else.
hurstshifter wrote:Not sure who this was being directed at but I don't think anyone in this thread has expressed the need to use every feature of the language nor has anyone claimed that they are better programmers for knowing polymorphism. As for me, I literally just learned this concept and it was interesting for me as I am very new to OO design. I think that people looking down on other programmers who over-utilize OO design techniques is just as counter-productive as the OO-abusers themselves. While it is definitely important to know when and where to use each feature of the language, it's also important to know how to use these features. I think we can agree on that.
I agree.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Polymorphism

Post by dandymcgee »

Apoc wrote: You seem to have quite a high-horse attitude. By no means did I state: 'You're not using OOP to it's fullest. Go use C'. I stated a fact, and an opinion. Then gave an example of how to use something he isn't quite familiar with. (Assuming so at least)
I understand inheritance perfectly, I simply said I haven't come across a reason to use it in anything I've worked on thus far.
I also mentioned that I'm fully aware that it has it's uses and that I could have probably used it somewhere as a better alternative to whatever I did use.

I still have yet to see an example of inheritance at work as a solution to a real-world problem that doesn't involve cars or banking. Perhaps if you could think of one it would help to enlighten me as to how I could properly implement this design pattern in my own projects. :|

Edit: That last statement wasn't sarcasm, although it may have sounded like it. It was a genuine request for knowledge. :mrgreen:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Polymorphism

Post by programmerinprogress »

We actually had an interesting post about how polymorphism and inheritance would be useful in creating your own objects in a GUI, using a standard interface for many objects, with differently implemented methods could make things a hell of a lot easier if you do it well, not to mention the reduction in the amount of code you have to write.

I'm still playing around with polymorphism, but I can't confidently say that i'm actively usingit correctly yet in my current projects.

PS: Apoc, I feel like i've heard of you before, did you used to be on the lionhead forums? I was quite active there under another alias.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Polymorphism

Post by avansc »

Apoc wrote:
avansc wrote:this is exactly why there is a influx of sub par programmers. they think that using every feature of language,
or implementing every know design pattern or design methodology makes them great programmers, or have
the attitude that if someone doesn't they are sub par programmers or should stick to a "other" language...
You seem to have quite a high-horse attitude. By no means did I state: 'You're not using OOP to it's fullest. Go use C'. I stated a fact, and an opinion. Then gave an example of how to use something he isn't quite familiar with. (Assuming so at least)

Using every feature of the language, or every known design pattern/methodology, usually means your code is over-complicated (especially when you consider C++).

Inheritance and polymorphism is what makes OOP so useful.

Also, I'm not sure how you have the knowledge, or ability, to call someone a sub-par programmer. Especially when you have no idea who I am, or what I've done in the past. Sure, I could be a newcomer to programming. Then again, I could be Bjarne. How would you know? Take your righteous attitude and give it to someone else.
hurstshifter wrote:Not sure who this was being directed at but I don't think anyone in this thread has expressed the need to use every feature of the language nor has anyone claimed that they are better programmers for knowing polymorphism. As for me, I literally just learned this concept and it was interesting for me as I am very new to OO design. I think that people looking down on other programmers who over-utilize OO design techniques is just as counter-productive as the OO-abusers themselves. While it is definitely important to know when and where to use each feature of the language, it's also important to know how to use these features. I think we can agree on that.
I agree.
you are delusional. in no way did i implicitly state that you are a sub par programmer. just that OO MANIA, has produced a slew of sub par programmers.

also... way to take it out of context... look at the edit. and that is specifically what i was referring to.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Polymorphism

Post by Bakkon »

My favorite use of it is the fact that all descendants can be held in a list/vector the requires an object of the original parent. It makes object management extremely simple and clean.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Polymorphism

Post by programmerinprogress »

you would store lists of pointers to the base class right?

sounds like a plan :lol:

Actually makes sense if you can just call a generic Draw() method, or something like that if the vector/list stored pointers to the different objects of the sub classes, means you could iterate through just one list as opposed to individuals, sounds a lot cleaner and more convenient.

I think you have to get the balance right though, if you're constantly focused on perfect OO design, and you're not actually getting anything out of it, you might as well be programming in a more convenient manner, but if you know how to harness the benefits of being able to treat different objects in a standard manner through a standard interface, then that's only a good thing, isn't it?
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Polymorphism

Post by Bakkon »

Yes, pointers. Oh god, your program would shit itself if you made copies of everything.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Polymorphism

Post by dandymcgee »

Bakkon wrote:My favorite use of it is the fact that all descendants can be held in a list/vector the requires an object of the original parent. It makes object management extremely simple and clean.
Thanks for that. Never thought of it before (I always forget you can treat derived class objects as if they were their parent's type).
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Polymorphism

Post by programmerinprogress »

I think that's the main idea of polymorphism.

Invoking methods with the same name, but taking "tailor-made" actions, this enforces the idea of "one interface, many methods", or indeed many forms from which the term is derived from.

In theory, you should be able to treat the object as if it was the base class, but it's actions reflect that of the subclass
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Polymorphism

Post by Falco Girgis »

dandymcgee wrote:
Bakkon wrote:My favorite use of it is the fact that all descendants can be held in a list/vector the requires an object of the original parent. It makes object management extremely simple and clean.
Thanks for that. Never thought of it before (I always forget you can treat derived class objects as if they were their parent's type).
That's pretty much one of the main instances that I use polymorphism.

Lots of times this doesn't even do much good. If you only want to do things with certain types (and they're all in the same list) and have to ignore a massive amount, you might as well have them in separate lists to begin with.

In Elysian Shadows, everything is polymorphed back into a collidable from its original derived class. Collidables are only good for collision. The collidable vector/container has only pointers. The actual players, NPCs, enemies, items, etc are in their own respective containers, so that I can also easily loop a player against every NPC (to see if they are chatting) or every item against every player (to see if they're close enough to pick it up).
dandymcgee wrote:I always forget you can treat derived class objects as if they were their parent's type
Haha, isn't that the basis of polymorphism?
Post Reply