Vishwas
Vishwas

Reputation: 1541

How to use a static constant (declared in parent class ) via a child class ( inherited class )

I have something like this :

class ParentClass
{
public static const ON_SOME_EVT:String = "onSomeEvent" ;
....
}


class ChildClass extends ParentClass
{
 ....
}



main()
{

trace( ChildClass.ON_SOME_EVT ) ; //<< compiler error on doing this
//1119: Access of possibly undefined property ABC through a reference with static type           Class.



} 

Then how should i achieve this. I want to access the constant via child class but not the parent class.

Thanks.

Upvotes: 1

Views: 529

Answers (3)

FlashJive.com
FlashJive.com

Reputation: 216

The best way to do this is just to re-declare your static const in the child class and reference the ParentClass.ON_SOME_EVT.

class ParentClass
{
public static const ON_SOME_EVT:String = "onSomeEvent" ;
....
}

class ChildClass extends ParentClass
{
public static const ON_SOME_EVT:STring = ParentClass.ON_SOME_EVT;
 ....
}

Upvotes: 1

www0z0k
www0z0k

Reputation: 4434

static vars can't be inherited

Upvotes: 2

I think you can just do trace(ON_SOME_EVT); in the ChildClass, because the constant is inherited too if I'm not mistaken.
But the constant is a static member of ParentClass so outside of the inheritance tree you cannot avoid using ParentClass.ON_SOME_EVT. Why don't you want to use that?

Upvotes: 1

Related Questions