Calling method but not overridden version

Monkey Forums/Monkey Programming/Calling method but not overridden version

Foppy(Posted 2012) [#1]
Suppose I have this:

Class MyBaseClass
   Method sayHello()
      Print "hello"
   End Method

   Method sayHelloAndNothingElse()
      sayHello()
   End Method
End

Class MyExtendedClass Extends MyBaseClass
   Method sayHello()
      Super.sayHello()
      Print "and hello again"
   End Method
End

Local bob:MyExtendedClass = New MyExtendedClass()

bob.sayHelloAndNothingElse()


This would print "hello and hello again", because sayHello of the extended class will first call sayHello from the base class, and then add more text.

But suppose that in method sayHelloAndNothingElse() in the base class, I really want to call only sayHello() as defined in the base class, ignoring the version of sayHello in the extended class. Is this possible?

It may seem a strange thing to want to do. The actual case in my game is that I have a list of entities. From the entity class, a unit class is extended. All entities are in a list of entities, but units are also in their own list. When I remove an entity, the unit should also remove itself from the unit list, so the removeFromList method in entity is overridden in unit to also remove the unit from the list of units. But sometimes, I want to remove an entity, but the unit should NOT remove itself from the unit list. (I am removing and reinserting entities to achieve depth sorting, which should leave the unit list unaffected.) So in that case I want to call ONLY the removeFromList method in the entity class, and ignore/not call the extended version of that method in the unit class. For the moment, I have "solved" it by duplicating the method in the entity class, giving it another name, for this special use.


NoOdle(Posted 2012) [#2]
How are you removing the objects from a list? Are you storing the link and removing that or calling RemoveEach on the list? If its the second, you might be able to simplify the problem by passing the list you wish the entity to be removed from to the method...
Method RemoveFromMyList( this : List< BaseObject > )
     this.RemoveEach( self )
End Method



Skn3(Posted 2012) [#3]
Can you cast the object to its super before calling the method?
E.g.

MyBaseClass(extendedInstance1).sayHello()



Jesse(Posted 2012) [#4]
No, it doesn't work, you still get the extended class methods which makes cense. I am also at a loss here as I tried to access the base class in a similar way as Foppy and wasn't able to figure it out and don't know if its a bug or is expected behavior.


muddy_shoes(Posted 2012) [#5]
For the moment, I have "solved" it by duplicating the method in the entity class, giving it another name, for this special use.


I'd say that you shouldn't be duplicating it -- you should be splitting it. Your example seems to fairly clearly demonstrate the problem with the structure in that you've got a method called SayHello that you then override to do more than SayHello. If you split the methods into SayOnlyHello, SayMore and a collective SayGreeting then you'd be able to call what you wanted.




Foppy(Posted 2012) [#6]
Thanks all for thinking about this and replying!

Muddy_shoes, you are right, splitting up the function helped me out!

As you said, at the base class level I had to split up the functionality into several methods, so that I could call upon the method I needed.

Class Entity
   Method remove()
      removeFromEntityList()
   End Method

   Method removeFromEntityList()
      ' code to remove object from entity list
   End Method
End

Class Unit Extends Entity
   Method remove()
      Super.remove()
      removeFromUnitList()
   End Method

   Method removeFromUnitList()
      ' code to remove object from unit list
   End Method
End


Now I can also call specifically removeFromEntityList in the Entity class, and it will not do anything with the unit list, because it is not an overridden function. Thanks again!

In my say hello example it would become this:
Class MyBaseClass
   Method sayHello()
      sayHelloAndNothingElse()
   End Method

   Method sayHelloAndNothingElse()
      Print "hello"
   End Method
End

Class MyExtendedClass Extends MyBaseClass
   Method sayHello()
      Super.sayHello()
      Print "and hello again"
   End Method
End

Local bob:MyExtendedClass = New MyExtendedClass()

bob.sayHelloAndNothingElse()

Which would then output "hello".