Inheritance and polymorphism
Inheritance
Inheritance is an object-oriented programming concept that allows you to define new classes that extend existing ones,
that is, the new class inherits all the members of its ancestor and adds some new members.
Instances of the child class are also considered by AddyScript to be instances of the parent class
(meaning that if B inherits from A, where b is an instance of B, then the expression b is A evaluates to true).
In the example below, we will illustrate how inheritance is handled in AddyScript by creating
a subclass of the Person class introduced in the previous section.
The example also shows how to invoke the parent class's constructor (to initialize inherited fields).
Mr. Steve, Male person aged 32 works for us since 23 years
He is currently a Senior Analyst at the IT department
The so defined Employee class inherits all the members of the Person class in addition to those that it declares itself.
Polymorphism
Polymorphism is nothing more than the ability to override inherited members in derived classes. Since AddyScript uses duck typing (i.e.: objects are generally taken for what they appear to be without further checking), it handles polymorphism without any additional semantics. Any class method is overridable unless it is marked as private, final, or static in the parent class. Also, when the parent class is abstract, any subclass that is not abstract must override its abstract properties and methods.
Note: the above example would work even if Cat, Dog and Pig were not declared as subclasses of Pet. That is a consequence of AddyScript's duck typing philosophy: an instance of any class that exposes a public instance cry method without any parameters can be used where a Pet is expected.
The super keyword
Even after you have overridden a method, you still can refer to its original implementation as super::methodName
(methodName being the name of that method). The same rule applies for properties and indexers.
Protocols
AddyScript has a number of protocols that, if implemented by a user-defined class allow instances of that class to behave like some of the predefined data types in certain aspects.
The iterator protocol:
First approach: moveFirst, hasNext, moveNext
The iterator protocol makes it possible to iterate over instances of a user-defined class with a foreach loop. For a class to implement this protocol, it must expose 3 methods named moveFirst, hasNext, and moveNext respectively. The role of moveFirst is to position the internal cursor on the first logical element of the collection (assuming your class is a custom collection). hasNext is supposed to return a boolean indicating whether the iteration can continue or not. Finally, moveNext is responsible for moving the internal cursor forward, returning the value pointed to by the cursor at each step.
Second approach: iterator method with yield statements
Alternatively, a class can implement the iterator protocol simply by exposing an iterator method whose body contains a succession of yield statements (presumably in a loop). The yield statement has a syntax similar to that of a throw statement or a parameterized return statement, but it is not actually a jump. It only tells AddyScript what value the iterator being constructed should return on each iteration.