I wouldn't suggest you uptype to an IClosable interface. By doing so,
you loose access to all the members of the UIComponent. And having
access to super class's members is why you do inheritance in the first
place.

say:

class Sub extends UIComponent implements IClosable

var sub1:IClosable = new Sub();
sub1.id; // You get an error here
sub1.isClosed; // this works

var sub2:UIComponent = new Sub();
sub2.id; // this works
sub2.isClosed; // You get an error here


I suggest you create a subclass of UIComponent, say AbstractUIComp*,
which will by default have all the members of UIComponent, plus all
the new members you want to add on your own (isClosed). Then you just
make more subclasses of the AbstractUIComp and you can safely uptype
them later to AbstractUIComp to get the polymorphism working.

say:

class AbstractUIComp extends UIComponent
class Sub extends AbstractUIComp 

var sub:AbstractUIComp = new Sub();
sub.id; // this works
sub.isClosed; // this works


* Unfortunately Abstract classes and members are not supported by AS3,
so you have to just "know" that it is an Abstract class (class that
you don't instantiate, you just use it for subclassing it). It would
also be a bit more elegant if you would implement IClosable for the
AbstractUIComp and all of it's classes. 

Cheers


--- In flexcoders@yahoogroups.com, "dsds99" <[EMAIL PROTECTED]> wrote:
>
> Thanks, your solution worked.
> 
> If I think about it, I'm puzzled. Just like UIComponent, the IClosable
> interface doesn't have the isClosed property either. So when I cast it
> that interface, it should fail but it doesn't. 
> 
> If that's how interfaces work. Then casting it as IUIComponent
> interface should have worked too. 
> 
> --- In flexcoders@yahoogroups.com, "Mike Krotscheck" <mkrotscheck@>
> wrote:
> >
> > UIComponent doesn't have a property called isClosed, therefore your
> > attempt to access that property on an instance cast to UIComponent
will
> > throw an error. What you need to do is have each class implement an
> > interface called IClosable or something along those lines, and cast to
> > that.
> >  
> > public function checkDoor(c:IClosable):Boolean
> > {
> >     return c.isClosed;
> > }
> >  
> > 
> > Michael Krotscheck
> > 
> > Senior Developer
> > 
> >                           
> > 
> > RESOURCE INTERACTIVE
> > 
> > <http://www.resource.com/> www.resource.com
<http://www.resource.com/> 
> > 
> > mkrotscheck@ <mailto:mkrotscheck@> 
> > 
> >  
> > 
> > ________________________________
> > 
> > From: flexcoders@yahoogroups.com
[mailto:[EMAIL PROTECTED] On
> > Behalf Of dsds99
> > Sent: Tuesday, February 19, 2008 4:54 PM
> > To: flexcoders@yahoogroups.com
> > Subject: [flexcoders] Polymorphism....?
> > 
> > 
> > 
> > I have 2 unrelated classes that extend UIComponent ultimately..;)
> > and each class implements a boolean property called "isClosed"
> > 
> > Passing these classes into the function as untyped. 
> > 
> > If I don't cast, it works. Trying to understand why it doesn't work
> > this way.
> > 
> > A compile time error occurs in the if statement. But isn't this where
> > polymorphism kicks in even though I am casting it to a higher class in
> > the chain.
> > 
> > public function checkDoor(c:*):void{
> > if((c is UIComponent).isClosed == true){
> > trace("opened");
> > }
> > }
> > 
> > 
> > 
> >  
> > 
> > 
> > We support privacy and confidentiality. Please delete this email if it
> > was received in error.
> > 
> > What's new ::
> > Capitalize on the social web | The Open Brand, a new book by Kelly
> > Mooney and Dr. Nita Rollins, available March 2008 |
www.theopenbrand.com
> >
>


Reply via email to