HaxeDoc2

(やまだぃちぅ) #1
It is not necessary for child classes to define their own constructors, but if they do, a call to
super()is mandatory. Unlike some other object-oriented languages, this call can appear any-
where in the constructor code and does not have to be the first expression.
A class may override methods (4.3) of its parent class, which requires the explicitoverride
keyword. The effects and restrictions of this are detailed inOverriding Methods(Section 4.3.1).

2.3.3 Interfaces


An interface can be understood as the signature of a class because it describes the public fields of
a class. Interfaces do not provide implementations but pure structural information:
1 interface Printable {
2 public function toString():String;
3 }
The syntax is similar to classes, with the following exceptions:


  • interfacekeyword is used instead ofclasskeyword

  • functions do not have any expressions ( 5 )

  • every field must have an explicit type


Interfaces, unlike structural subtyping (3.5.2), describe astatic relationbetween classes. A given
class is only considered to be compatible to an interface if it explicitly states so:
1 class Point implements Printable{}
Here, theimplementskeyword denotes thatPointhas a ”is-a” relationship toPrintable,
i.e. each instance ofPointis also an instance ofPrintable. While a class may only have one
parent class, it may implement multiple interfaces through multipleimplementskeywords:
1 class Point implements Printable
2 implements Serializable
The compiler checks if theimplementsassumption holds. That is, it makes sure the class ac-
tually does implement all the fields required by the interface. A field is considered implemented
if the class or any of its parent classes provide an implementation.
Interface fields are not limited to methods. They can be variables and properties as well:
1 interface Placeable {
2 public var x:Float;
3 public var y:Float;
4 }
5
6 class Main implements Placeable {
7 public var x:Float;
8 public var y:Float;
9 static public function main() { }
10 }


Trivia: Implements Syntax
Haxe versions prior to 3.0 required multipleimplementskeywords to be separated by a
comma. We decided to adhere to the de-facto standard of Java and got rid of the comma.
This was one of the breaking changes between Haxe 2 and 3.
Free download pdf