Emojicode Documentation 1.0 beta 2

Inheritance and Overriding

Inheritance allows instances of a more concrete 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 with 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.

You cannot override generic methods and the compiler will never consider a generic method the super-method of another method.

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.

Final Classes

The attribute πŸ” marks a class as final. A final class cannot be subclassed or an compiler error will be raised.

Hint

Marking a class as final not only makes your intent clear but can also lead to performance improvements. Although the compiler tries to automatically detect final classes, it cannot do so in packages that export types.

The following example will raise a compiler error as 🐟 is attributed with πŸ”.

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

πŸ‡ 🐑 🐟 πŸ‡

πŸ‰

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