nww04
nww04

Reputation: 1857

Overriding a function still calls parent class before child method

I am new to Haxe and only plans to deploy for the web.

So I have a class A which has a method name doThis(). On class B, I inherited class A. I override doThis() on class B. When I check on the debugger, class A doThis() is being called and then class B doThis() is called as well.

My intuition is, I have overridden the methods explicitly and the only way I can call the parent is via a super.doThis() but it seems it does this automatically. I only want the version of doThis() to be B. Not A's.

Any thought on why it behaves like this? I think I a missing something here.

Thanks!

Upvotes: 0

Views: 288

Answers (3)

kobi7
kobi7

Reputation: 971

I think you're doing the right thing.
perhaps add some println to the parent function and the child function to make sure what you describe is what actually happens.

In other words, the debugger is not just playing with you.

Upvotes: 0

Jeff Ward
Jeff Ward

Reputation: 19146

Without any further information, I'd bet good money that you have your debugger breakpoints on the definition of doThis, when you meant to put them in the invocation of doThis (inside the function body).

Other possible (but less likely) reasons:

  • A macro function is inserting a super.doThis() call
  • A modified Haxe compiler or JS generator is emitting a super.doThis() call.

Upvotes: 5

5Mixer
5Mixer

Reputation: 531

Unless the function you are overriding is the constructor function new, calling the parent (super) function is optional. Furthermore, you can stipulate when the parent function is called by adjusting when you call super.doThis().

To illustrate, here is code that only runs the child classes function on try.haxe. It sounds like you may have already tried a similar approach, so ensure that you aren't missing some code that you may not be aware is calling the super function.

Upvotes: 2

Related Questions