What is "Override"?

Home Forums General Programming Discussion What is "Override"?

This topic contains 5 replies, has 4 voices, and was last updated by  abakobo 5 months, 2 weeks ago.

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

    GfK
    Participant

    I see this quite a lot in sample code (in method declarations), but it isn’t mentioned anywhere in the documentation.

    What does it mean?

    #8534

    Danilo
    Participant

    ‘Override’ replaces a ‘Virtual’ method by a new method.

    Class X has a virtual method doSomething().
    Class Y inherits/extends class X.
    Class Y would like to change the method doSomething(),
    so it overrides it. When calling Y.doSomething(),
    the overriden/replaced method is executed.

    Without the keyword ‘Override’ you could replace a
    method by accident, just by using the same method
    fingerprint (method name + parameters).
    To avoid such accidents, you have to explicitely
    use ‘Override’ to make sure it is really what you want
    to do.

    Search for “OOP override”: https://en.wikipedia.org/wiki/Method_overriding

    #8551

    Mark Sibly
    Keymaster

    Ugh, docs don’t even mention methods – will fix NOW!

    But as Danilo says, it’s basically indicates you’re ‘changing’ the behaviour of a method in a super class (which is usually declared ‘Virtual’, indicating it can be overridden).

    This is actually the default behavior in blitzmax/monkey, where all methods are virtual/override by default. In monkey2 you must explictly declare which methods can be overriden by subclass methods (with ‘Virtual’), and which methods are overriding superclass methods (with ‘Override’).

    #8557

    GfK
    Participant

    Ok that makes sense.  Thanks for the info.

     

    As an aside, is there any documentation I can read to familiarise myself with monkey2, without asking a ton of noob questions?

    #8558

    Mark Sibly
    Keymaster

    There’s just the ‘docs’ and what’s here in the forums. But don’t be afraid to ask noob Qs, they’re usually quick and easy to answer and will help others out.

    #8576

    abakobo
    Participant

    I started to work on the docs to add some missing think. There’s a keyword index where you would have had a brief answer to the question. It is in total WIP but contains all the official infos + some I added.

    http://turdus.be/monkey2docs/docs/

    note that you can call the super method in you override with the Super keyword so you don’t have to rewrite all the super methods code.

    [/crayon]

    there’s also the Pakz exmples that are great to start with https://github.com/Pakz001/Monkey2examples

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

You must be logged in to reply to this topic.