Hi,
I have a problem with some classes that I am working with and I would like to hear your opinions as well as your explanations as to why this is occurring.
Taking into consideration the following 2 classes, “ParentClass” and “ChildClass”, one extending the other:
package
{
public class ParentClass
{
protected var label:Object;
public function ParentClass()
{
trace(label);
// The label will be undefined.
}
}
}
package
{
public class ChildClass extends ParentClass
{
public function ChildClass()
{
label = new Object();
super();
}
}
}
In the above code, the “label” variable is defined in the parent class, but created in the child class.
In this case, the “label” variable will be accessible in the child class only. When I try to call it from the parent class, it will be undefined.
Why is this ? Since the variable is defined in the parent class, shouldn’t it be accessible in the parent class, after it is created in the child class ?
Also, to further extend why this situation seems weird to me, if I create the object when declaring it, in the parent class, and afterward create it again in the child class, thus substituting the original object, then it will be accessible from the parent class.
In the following slightly modified example, the “label” variable will be accessible in the parent class:
package
{
public class ParentClass
{
protected var label:Object = new Object();
public function ParentClass()
{
trace(label);
// The label will be accessible.
}
}
}
package
{
public class ChildClass extends ParentClass
{
public function ChildClass()
{
label = new Object();
super();
}
}
}
I am looking forward to your replies.
Thank you.