Extended Class is Calling Parent Class Methods

Monkey Forums/Monkey Programming/Extended Class is Calling Parent Class Methods

zoqfotpik(Posted 2013) [#1]
I have a class hierarchy. The base class contains a "drawall" function, which is there for encapsulation, and calls a "drawme" method which should be overridden in classes extending this base class.

However, this is not the case-- the parent class is being called and not the child class

Moreover, if I put a "myimage" field in the parent class, have the drawme method attempt to draw that, and then attempt to override that field in the child class (that is to say, set it to point to a different image), the drawme method of the parent class is called and still draws the myimage of the parent class.

The drawall function in the parent class simply walks the entity list and calls drawme for each existing entity.

Anyone know what might be occurring here?


Jesse(Posted 2013) [#2]
I am not sure I understand what you are saying but I am going to take a wild guess.
The problem is if you call any method from the base class and if you have a method in the base class with the same name as the extended class the base method will take precedence over the extended method as the base class doesn't have any knowledge of the extended methods. you can only call extended methods from extended classes not from the base class.

Try making the base method as "abstract" and see if that fixes it.

Method drawme() Abstract



if that doesn't work than you might have to post some code to illustrate the problem.
I have a lot of my programming using inheritance and polymorphy and never had that problem so it might be the way you are handling the objects that's the problem.


zoqfotpik(Posted 2013) [#3]
The problem was that I had a factory method used to create the parent instance which was being called but not overridden in the child class.

Speaking of which, how does one create an abstract factory function? I have a function called createwave:void(num:int) which creates a number of entities equal to num-- how can I have it look like createwave:void(num:int, protoentity:entity) where it creates a number of entities similar to the entity that is being fed into it, but where they might have been subclassed from the main entity type?


Jesse(Posted 2013) [#4]
maybe:

Global AliensList:List<Base> = New List<Base>

Class Aliens1 extends Base
.
.
End Class

Class Aliens2 Extends Base
.
.
End Class
Class Factory<T>

       Function CreateWave(num:int)
          For local i:int = 0 until num
               alienslist.Addlast( new T)
          Next
       End Function
End Class


Factory<Aliens1>.CreateWave(5)
Factory<Aliens2>.CreateWave(10)




zoqfotpik(Posted 2013) [#5]
That's what I was looking for. Cheers!

Coming here from primarily c-style programming has been quite the learning experience. I'm sure glad I started on Blitzmax two years back or this would all be a bit much.