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 Language Reference & Guide
- Welcome to Emojicode
- Syntax
- The Basics
- Literals
- Variables and Assignment
- Control Flow
- Classes & Value Types
- Overloading
- Operators
- Optionals
- Errors
- Inheritance and Overriding
- Protocols
- Enumerations
- Types and Namespaces
- Types as Values
- Documentation
- Generics
- Callables
- Packages
- Threads
- Safe and Unsafe Code
- Memory Management
- References
- Appendix: The Emojicode Compiler
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:
- 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.
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.
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:
- The method or initializer of the subclass must take the same number of parameters.
- 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 parameters 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.
- If the super method or initializer is not escaping, the overriding method or initializer may not be escaping.
- If a parameter of the overriding method or initializer is escaping, so must be the respective parameter of the super method or initializer.