From what I read, interfaces are basically classes for methods, right? If two classes implements the same interface, then they should both have methods described in the interface.
Now, how is this useful? Let’s say I want to call foo();.
public interface IExample {
function foo(om:String):void;
}
class HungryClass implements IExample{
public function foo(om:String):void{
trace("OM NOM NOM!!! Thank you for feeding me" + om);
}
}
class FullClass implements IExample{
public function foo(om:String):void{
trace("No thanks, I don't want to eat" + om);
}
}
//somewhere..
instanceOfEitherClass.foo("cake");
How does interfacing help? Wouldn’t this work without interfacing?
Thanks
Lets say you have a concrete class that inherits from an abstract class. In that case you would simply do the following:
Now how about if you need the concrete class to also inherit from the
EventDispatcherclass? You can’t do the following:However you can implement the
EventDispatcherclass’sIEventDispatcherinterface and then use anEventDispatcherobject like the following:Using this combination of composition and interfaces you can use the concrete class as both an
AbstractClassandEventDispatcherobject.Interfaces are great for allowing “unrelated objects to communicate with one another”. You can find more information on interfaces here.