Override question

About Monkey 2 Forums Monkey 2 Programming Help Override question

This topic contains 5 replies, has 4 voices, and was last updated by  Xaron 2 years, 3 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #6174

    Xaron
    Participant

    Just a simple question. When overriding a method A the method A in the base class is NOT called right? So when I want to call the base method first I have to call Super.A() ?

    Is that true for the constructor (New()) as well?
    Thanks!

    #6178

    codifies
    Participant

    Thought you could only override an abstract (which is a shame!)

    extended new calls base new…

    [/crayon]
    #6181

    Xaron
    Participant

    Thanks. I’ve extended your example. All in all base constructors are called while base methods are not (no matter if I use virtual/override for New()).

    Results:

    base new
    —–
    base new
    extended new
    —–
    base method
    —–
    extended method
    —–

    #6187

    abakobo
    Participant

    For me it makes sense because the default constructor is the initialization. And every base ‘typed’ objects should be initialized properly..
    But sometimes it not logical, in the following example the new(i:int) from base does not call default constructor but new(i:int) from extended does…
    Dunno what to think about this

    [/crayon]
    #6223

    Mark Sibly
    Keymaster

    When overriding a method, yes, you need to call Super.Blah or it’ll never be called.

    When writing a constructor, you can user Super.New(…) to explicitly call a super class constructor, in which case the call to Super.New must be the first statement in the constructor.

    If your constructor does not call Super.New(…) the super class must have a ‘default’ constructor  – ie: a constructor with no params – and this will be called for you before your constructor runs. Note that a default ‘NOP’ constructor will be automatically generated for any class with NO constructors.

    In monkey2, you can also use Self.New instead of Super.New at the start of a constructor. This will call a constructor in the same class instead of a super class constructor. A constructor can only call Self.New or Super.New but not both.

    #6224

    Xaron
    Participant

    Thanks for clarification, Mark!

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.