Polymorphism in monkey?

Monkey Forums/Monkey Programming/Polymorphism in monkey?

taumel(Posted 2011) [#1]
How does polymorphism work in monkey? Is it supported? For instance for organising different types of objects.

Maybe it's just too late as well.


GW_(Posted 2011) [#2]
The Monkey docs talks about generics a bit, so it is supported to some degree.


Brucey(Posted 2011) [#3]
There's classes with superclasses, and casting between them...
But there's no interfaces or multiple inheritence - although "interface" is a reserved keyword apparently.


taumel(Posted 2011) [#4]
Still tired...

Do you have any example with the usual apple and banana, both declared as fruits?

So far all worked out fine with building classes out of classes, extending them and using abstract methods (although reserved you must not use it) but this way you can't access variables from the apples and bananas which aren't defined in the fruits class anymore.


Wagenheimer(Posted 2011) [#5]
The child classes does not inherites the master methods?

Class Sprite
Method New(img:Image,x#, y#,frame:Int=0)
Self.image = img
Self.x = x
Self.y = y
Self.w = img.Width()
Self.h = img.Height()
Self.w2 = Self.w/2
Self.h2 = Self.h/2
Self.currentframe=frame
End
EndIf

Class Amebo extends Sprite
Endif

Local a:Amebo=New Amebo(image_amebo,100,100,0) reaises me a "Unable to find overload for new(expression,100,100,0)

I have not changed the new method in the Amebo Class, Can't I use the new method from the Sprite class?


dopeyrulz(Posted 2011) [#6]
Wagenheimer,

You will need to call the base class from the New constructor like this:

Class Amebo extends Sprite

Method New()
   Super.New(image_amebo,100,100,0)
End Method

Endif



Wagenheimer(Posted 2011) [#7]
Thanks dopeyrulz! Works very well! =)


wiebow(Posted 2011) [#8]
Oh, this used to happen automatically in blitmax, afaik.


Warpy(Posted 2011) [#9]
It happened automatically when New didn't accept any parameters, and that's still the case in Monkey - if you define a New() method in the parent class with no parameters, that automatically gets called.