Beginning ActionScript 3.0 – Interface

Bear-User-256x256 So, when you fill the blank, eh, which we discuss in the polymorphism, the concrete instance should be a Person, a Cook, an Author or a Driver. Have you ever think that what if the concrete class can’t be Person? That is the concrete class should be the subclasses, not the super one.


In other words, this kind of code segment:

var person:Person = new Person();

won’t appear in our code.

Now, the definition of haveBreakfast seems to be useless, isn’t it?

All we need is just the declaration, not definition, we don’t need the definition, because we won’t use it, but we need the declaration, we need it, so we can do upcasting, and polymorphism.

When you have the same trouble, I mean you need to declaration some methods but you don’t need to implement them, then you need the interface. An interface is a collection of method declarations that allows unrelated objects to communicate with one another.

Before more discussion, let’s get to know how to define interface first.

Here is a simple example for defining the interface.

package {
public interface IHaveMeals{
function haveBreakfast():void;

Compare to the class definition, there’re some differences.

1st, the interface is declared with the public access control modifier. Interface definitions may only be modified by the public and internal access control specifiers. So, before the keyword “interface”, you can just use “public” or “internal”.

2nd, the method declarations inside an interface definition cannot have any access control specifiers. That means don’t use “public”, “private” or such access control specifiers before the keyword “function”.

3rd, remember, all we need is just some method declarations; we don’t need the implementation, so leave the curly braces alone.

After the definition, let’s turn to how to use it.

In order to make the code runs smoothly, let’s delete the haveBreakfast method in the Person class first.

For the subclasses, they need to implement the interface.

So, let’s add something to the code.

For the Cook class,

public class Cook extends Person

changes into:

public class Cook extends Person implements IHaveMeals

The same for the Author class.

The keyword “implements” means the subclass is inherited from an interface, or implements the interface.

Then, turn to the method haveBreakfast, and delete the keyword “override”. When you implement the interface, you don’t need to override, so, delete it.

Write some test code.

var person:IHaveMeals = new Author();

person = new Cook()

And here is the result.


Implements the interface is much the same with inherited from the superclass, while they also have many difference.

1st, i general, interface has a more weak semantic meaning. You know, inheritance refers to “is a”, while interface is “like a”.

For example, the alarm door is a door, but like an alarm.

So, the definition of “alarm door” maybe likes this:

Public class AlarmDoor extends Door implements IAlarm.

2nd, Interfaces cannot include variables or constants but can include getters and setters.

One more thing, ActionScript 3.0 follows a convention in which interface names begin with an uppercase I.

Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

Leave a Reply