Super
Super( ...param : any )
Super : Object
Parameter | Type | Description | |
---|---|---|---|
param | any | -> | Parameter(s) to pass to the parent constructor |
Result | Object | <- | Object's parent |
The Super
keyword allows calls to the superclass
, i.e. the parent class.
Super
serves two different purposes:
- Inside a [constructor code]((../Concepts/classes.md#class-constructor),
Super
is a command that allows to call the constructor of the superclass. When used in a constructor, theSuper
command appears alone and must be used before theThis
keyword is used.
- If all class constructors in the inheritance tree are not properly called, error -10748 is generated. It's 4D developer to make sure calls are valid.
- If the
This
command is called on an object whose superclasses have not been constructed, error -10743 is generated. - If
Super
is called out of an object scope, or on an object whose superclass constructor has already been called, error -10746 is generated.
// inside myClass constructor
var $text1; $text2 : Text
Super($text1) //calls superclass constructor with a text param
This.param:=$text2 // use second param
- Inside a class function,
Super
designates the prototype of thesuperclass
and allows to call a function of the superclass hierarchy.
Super.doSomething(42) //calls "doSomething" function
//declared in superclasses
Example 1
This example illustrates the use of Super
in a class constructor. The command is called to avoid duplicating the constructor parts that are common between Rectangle
and Square
classes.
// Class: Rectangle
Class constructor($width : Integer; $height : Integer)
This.name:="Rectangle"
This.height:=$height
This.width:=$width
Function sayName()
ALERT("Hi, I am a "+This.name+".")
// Function definition
Function getArea() : Integer
return (This.height)*(This.width)
//Class: Square
Class extends Rectangle
Class constructor ($side : Integer)
// It calls the parent class's constructor with lengths
// provided for the Rectangle's width and height
Super($side;$side)
// In derived classes, Super must be called
// before you can use 'This'
This.name:="Square"
Function getArea() : Integer
return This.height*This.width
Example 2
This example illustrates the use of Super
in a class function. You created the Rectangle
class with a function:
//Class: Rectangle
Function nbSides() : Text
return "I have 4 sides"
You also created the Square
class with a function calling the superclass function:
//Class: Square
Class extends Rectangle
Function description() : Text
return Super.nbSides()+" which are all equal"
Then you can write in a project method:
var $square : Object
var $message : Text
$square:=cs.Square.new()
$message:=$square.description() //I have 4 sides which are all equal
See also
Properties
Command number | 1706 |
Thread safe | ✓ |