Game Development Reference
In-Depth Information
and coding is to extend the first class to a new class, known as a sub-
class . All public and protected methods and variables that are not
static will be available to the new class. To clarify, any static proper-
ties of the parent, or superclass , must be prefaced with the class
name (as in the example below). In addition, any internal methods
or variables will be available to the subclass if it is in the same pack-
age as its superclass. To illustrate, let
'
s look at an example below:
package {
public class SuperClass {
static public var className:String = " SuperClass " ;
}
}
package {
public class SubClass extends SuperClass {
public function SubClass() {
trace(SuperClass.className); //OUTPUTS
"
"
trace(className); //THROWS ERROR
SuperClass
}
}
}
//FROM OUTSIDE EITHER CLASS
trace(SuperClass.className); //OUTPUTS " SuperClass "
trace(SubClass.className); //THROWS ERROR
ll need to change the functionality of a
method in a subclass from the way it behaves in the superclass.
This change in functionality through inheritance is known as
polymorphism .Youcandothisusingthe override keyword before
the beginning of the method, albeit with a number of caveats.
￿
Occasionally, you
'
Only methods may be overridden; no properties
￿
Only public, protected, and internal methods may be overridden
￿
Internal methods may only be overridden in subclasses in the
same package as the superclass
￿
The new overriding method must match the composition of the
original method, with the same parameters and return value
Let
'
s look at an example.
package {
class SuperClass {
public var name:String = " SuperClass " ;
protected var _number:Number = 5;
internal var _packageNumber:Number = 7.5;
private var _secretNumber:Number = 10;
Search Nedrilad ::




Custom Search