views:

1098

answers:

2
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Label id="lb" text="check"/>
    <mx:Script>
        <![CDATA[
            import mx.managers.BrowserManager;
            import mx.managers.IBrowserManager;
            public var brm:IBrowserManager = BrowserManager.getInstance();
        ]]>
    </mx:Script>
</mx:WindowedApplication>

In the above code, IBrowserManager is an interface, and BrowserManager.getInstance() returns an object of type IBrowserManager. But, from whatever I know of interfaces, one can't create an instance of an Interface? So, how come can I create an instance of an interface here?

+3  A: 

You don't create an instance of an interface. What you get there is an instance of a class that implements that interface. It's only natural that you can assign to a variable of type IBrowserManager, an instance of class BrowserManager which implements IBrowserManager.

bug-a-lot
+2  A: 

Let's say you had an interface, IPerson, that defined two members -- name and gender:

public interface IPerson
{
    function get name():String;
    function set name(value:String):void;

    function get gender:String;
    function set gender(value:String):void;
}

... and you had two concrete classes, Woman and Man, both of which implemented the IPerson interface.

public class Man implements IPerson
{
    private var _name:String;
    private var _gender:String;

    public function Man(name:String, gender:String)
    {
     this.name = name;
     this.gender = gender; 
    }

    public function get name():String
    {
     return _name;
    }

    public function set name(value:String):void
    {
     _name = value;
    }

    public function get gender()
    {
     return _gender;
    }

    public function set gender(value:String):void
    {
     _gender = value;
    }
}

and

public class Woman implements IPerson
{
    private var _name:String;
    private var _gender:String;

    public function Woman(name:String, gender:String)
    {
     this.name = name;
     this.gender = gender; 
    }

    public function get name():String
    {
     return _name;
    }

    public function set name(value:String):void
    {
     _name = value;
    }

    public function get gender()
    {
     return _gender;
    }

    public function set gender(value:String):void
    {
     _gender = value;
    }
}

You could then ultimately do something like this:

var crowdOfPeople:ArrayCollection = YourCrowdOfPeopleClass.getInstance();

for each (var p:IPerson in crowdOfPeople)
{
    trace(p.name);
    trace(p.gender);
}

... which is to say, act on (in this case, trace the properties of) the concrete implementations of Man and Woman as though they were the same kind of object, since they both conform to the IPerson interface (i.e., the both define name and gender properties).

It's the object-oriented principle of polymorphism at work -- sometimes you'll hear it as the tenet, "Program to interfaces, not implementations." Hope that helps!

Christian Nunciato