Search code examples
actionscript-3interfacetypechecking

AS3 How to make a variable hold only objects that implements a given interface


I have two classes, lines and points. Both class implements 'highlightable'. I want to make a variable that can hold different type of objects that implements the 'highlightable' interface.

var currentObject:lines; //Won't work. It can only hold 'lines' object.
var currentObject:points; //won't work because it can only hold 'points' object.
var currentObject:Object; //Won't work because it can hold any objects. I want it to hold only those objects that implements the 'highlightable' interface.

Is it possible?

Thanks


Solution

  • All you need to do is make your variable the type of your interface.

    var currentObject:highlightable = new classA();
    

    or

    var currentObject:highlightable = new classB();
    

    Where both classA and classB implement the highlightable interface.