Emojicode Documentation 0.8
Guides Language Reference Package Index

Inheritance and Overriding

Inheritance allows instances of a more concret class to be treated like instances of a less concrete class. When you subclass a class, the subclass inherits all methods and type methods. Initializers are only inherited under special conditions.

Initializer Inheritance

In Emojicode, initializers are only inherited by subclasses if the subclass meets these criteria:

If the subclass doesn’t fullfil these criteria, the subclass doesn’t inherit any initializers.

Required Initializers

Sometimes it’s desired that all subclasses of a class have a specific initializer. In this case, a required initializer can be used.

An initializer can be marked as required witt the πŸ”‘ attribute. Subclasses must implement all initializers marked with πŸ”‘ themselves if they aren't eligible for initializer inheritance. These initializer again must then be marked with πŸ”‘, thereby enforcing that all descendants of the original class provide the required intalizer.

Overriding Methods

A subclass can override a method defined in a superclass, that is providing a new implementation of it.

A method is overriden by redeclaring it in the subclass with the βœ’οΈ attribute. As for example:

πŸ‡ 🌼 πŸ‡
  ❗️ ⏰ time πŸ”’ πŸ‡
    πŸ’­ Open and close the blossom according to the time...
  πŸ‰
πŸ‰

πŸ‡ 🌻 🌼 πŸ‡
  βœ’οΈ ❗️ ⏰ time πŸ”’ πŸ‡
    πŸ’­ Sunflowers also rotate to face the sun....
    ‴️⏰ time  πŸ’­ Open and close like other flowers; see below
  πŸ‰
πŸ‰

The same logic applies to type methods.

Access Level and Overriding

If you override a method and do not specify an access level, the method inherits the overridden method’s access level. In the example below, πŸ‘β€™s πŸ™‹ method is πŸ” because no access level was specified:

πŸ‡ 🐟 πŸ‡
  πŸ†• πŸ‡πŸ‰

  πŸ” ❗️ πŸ™‹ πŸ‡
    πŸ˜€ πŸ”€I’m a fish.πŸ”€β—οΈ
  πŸ‰
πŸ‰

πŸ‡ 🐑 🐟 πŸ‡
  βœ’οΈβ—οΈ πŸ™‹ πŸ‡
    πŸ˜€ πŸ”€I’m a blowfish.πŸ”€β—οΈ
  πŸ‰
πŸ‰

An overriding method must be at least as acessible as the method it overrides. This means that you cannot make an overriding method πŸ”’, nor can you override a πŸ”“ method with a πŸ” method.

Calling Super Methods

Inside a method you can use this syntax to call the super method:

super ⟢ ‴️ emoji-id [arguments]

This simply calls the super method named method-emoji and returns it value. You have already seen an example above.

Promises

You must watch out not to break the superclass’s promises when overrding methods. Promises are a set of rules that ensure that the methods and required intializers of a class can be used the same way as the ones of the superclass – a main characteristic of object orientation. These promises are:

← Previous Next Up: β€œProtocols” β†’
Something not quite right? Improve this page