Polymorphism?

BlitzMax Forums/BlitzMax Beginners Area/Polymorphism?

BLaBZ(Posted 2009) [#1]
Is polymorphism common? how frequently do you use it?

Trying to wrap my head around this...


matibee(Posted 2009) [#2]
Is polymorphism common?

Yes.

how frequently do you use it?

Twice a day, everyday, except on Sundays.

But seriously, as a fundamental concept of OOP it's used a abused a lot. It comes with the experience of learning how to break your types/classes into a suitable heirarchy. For me that heirarchy should speed up development time (with reusable code), make a sensible client interface (a bit tricky with Blitz's lack of public/private/protected scope) and cut the risk of bugs (shared code should mean reusing proven code).

Don't worry about getting the heirarchy right at the start, dive in and code if it helps and learn to refactor.


plash(Posted 2009) [#3]
Is polymorphism common?
Yes it is.

how frequently do you use it?
Hmm.. I use it in all my modules (the entire duct tree, which is a lot of code).
So, a lot.

Trying to wrap my head around this...
Try this tutorial: http://www.blitzbasic.com/Community/posts.php?topic=78860


Gabriel(Posted 2009) [#4]
But seriously, as a fundamental concept of OOP it's used a abused a lot. It comes with the experience of learning how to break your types/classes into a suitable heirarchy. For me that heirarchy should speed up development time (with reusable code), make a sensible client interface (a bit tricky with Blitz's lack of public/private/protected scope) and cut the risk of bugs (shared code should mean reusing proven code).

But surely reusable code is the opposite of polymorphism? Polymorphism is having a uniform interface to two objects, despite the fact that the objects use different code under the hood. Reuseable code comes from inheritance, where two or more objects share the code of a "parent" object. There's no reason that you can't use polymorphism as well as inheritance, indeed that's probably the most common way to use it, but it doesn't necessarily follow that you're using inheritance just because you're using polymorphism. They're complementary but by no means interdependent.


Brucey(Posted 2009) [#5]
how frequently do you use it?

I've been known to use it on occasion.


matibee(Posted 2009) [#6]
Fair point Gabriel. My point about code reuse comes from a practical design point of view.. [to B-labz, I'm sure you get this Gabriel :)]..

With inheritance, it's important that a base type covers all the fundamentals before any (polym'd or not) type is derived from it. There's lots of text book examples of polymorphed classes that do very little other than show what can be done, but in practice you can end up with lots of base member data that needs taking care of too.

The extreme example of polymorphism is the pure virtual type that cannot exist as an object. It sets out standards and expected functionality for all derived types. I've very rarely come across these and that perspective did skew my previous reply.

I'm crap at examples or I'd chuck some in :)


BLaBZ(Posted 2009) [#7]
So you use polymorphism when you want to reference multiple classes of an "inherited" class correct?
Is this the most common way it's used?


Mahan(Posted 2009) [#8]
I think one very good example of polymorphisms is the Streams-lib that is in most languages today (bmx included).

The nice thing is that as long as you are writing to, or reading from, the abstract base-class TStream you don't have to know at that point in time what your code will be used with.

Right now, you might be using a TBankStream, and tomorrow you can reuse the same code in a compressed network stream or a filestream. It does not matter as far as the writing/reading code is concerned.


Gabriel(Posted 2009) [#9]
So you use polymorphism when you want to reference multiple classes of an "inherited" class correct?

Not automatically, only if the "inherit-from" class has abstract methods which *both* "inherit-to" implement. The point of polymorphism is this:

Type Parent
   Method DoSomething() Abstract
   Method DoSomethingElse() Abstract
End Type

Type ChildA
End Type

Type ChildB
End Type

Global A:Parent=CurrentLevel.GetParentByName("SomeObjectFromMyGame")
A.DoSomething()
A.DoSomethingElse()


I haven't implemented the methods for ChildA and ChildB, but notice that I only ever refer to A as a Parent. I'm telling the object to do something without even knowing whether it's a ChildA or a ChildB. That's polymorphism. In other words, it doesn't *matter* whether it's a ChildA or a ChildB because they both implement those methods. They can both do what I want them to do, they just do it in different ways. I don't know how they do it, and I don't *need* to know. That's polymorphism.

It's like having a radiator and a convector heater. I just plug them in, switch them on and they heat the room. They work in a completely different fashion, but I don't need to know how they work, or even know that they work differently. To me, I just say

Local Radiator:Heater=LivingRoom:GetHeater()
Radiator.PlugIn()
Radiator.SwitchOn()
Local Convector:Heater=Bedroom:GetHeater()
Convector.PlugIn()
Convector.SwitchOn()



GfK(Posted 2009) [#10]
I haven't the slightest idea what it is, but I probably use it on a daily basis.


Corum(Posted 2009) [#11]
BlitzMax polymorphism is fine, working and regularly used.
The problems come with other OOP features, such as class members scope and methods overloading, mostly messy and incomplete...