The Language Reference & Guide
- Welcome to Emojicode
- The Basics
- Variables and Assignment
- Control Flow
- The s package
- Classes & Value Types
- Inheritance and Overriding
- Types and Namespaces
- Types As Values
- Safe and Unsafe Code
- Memory Management
- Appendix: The Emojicode Compiler
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.
In Emojicode, initializers are only inherited by subclasses if the subclass meets these criteria:
- The subclass does not define any instance variables.
- The subclass does not define any initializer.
If the subclass doesn’t fullfil these criteria, the subclass doesn’t inherit any 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.
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:
This simply calls the super method named method-emoji and returns it value. You have already seen an example above.
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:
- The method or initializer of the subclass must take the same number of arguments.
- The return type of the method or initializer of the subclass must be the same or a subtype of the return type of the overriden method or intializer.
- The arguments of the method or initializer of the subclass must be of the same type or a super type of the argument types of the overriden method or intializer.
- The method or initializer of the subclass must have the same accessibility or be more accessible.