Home > Actionscript 3.0, Object Oriented Programming > AS3 – Object Oriented Programming – Part2

AS3 – Object Oriented Programming – Part2

Hi again,

It took me more time than I first thought to write this part down, since i’ve been very busy, but here it is.

As I said, I’ll talk about packages in this second part of the OOP tutorial.

So. What is a package?: Is a way of grouping things together and distinguish between things that look-a-like but come from different sources.
For example: we create our class Apple and a team collegue brings his own version of class Apple. This creates ambiguity for the compiler (and for us also) because it can’t decide which version of the Apple class to use. To avoid this ambiguity, without changing the name of any of the classes, we move them into separate packages: let’s say mypackage will hold our Apple class and colleguepackage will hold our collegue’s class.
To refer to the classes inside packages, we can use the name of the package, like this:
mypackage.Apple and colleguepackage.Apple
As you can see the “.” [dot] operator is used to access content inside packages. Remember however that classes declared internal are not accesible outside the package they are contained in!

Let’s see some code:

package mypackage {
  public class Apple {
  }
}

What we did: we create the package mypackage, and inside it we created the class Apple, public.

To use the class we need to import it:

import mypackage.Apple;

var myaple: Apple = new Apple();

Every class must be inside a package, but it’s worth noting that a global package exists. The classes inside the global package are available without importing them (still the access specifiers occur). To create a class inside the global package, don’t give any name to the package, like this:

package {
  public class Apple {
  }
}

Now the Apple class can be used directly:

var myApple : Apple = new Apple();

Packages can be nested. That means we can have a package named com and inside this package, we can have another package named com.otherpackage. Of course the nesting can go deeper, any package can hold other packages..
How do we create nested packages, as a regular package, only specifying the entire qualified name for that package. Example:
Define the com package that will hold a Test class:

package com {
  internal class Test {
  }
}

Define the com.otherpackage.mypackage that will hold the Apple class:

package com.otherpackage.mypackage {
  public class Apple {
  }
}

Looking at the com.otherpackage.mypackage package we can say:
com.otherpackage.mypackage is a sub-package of com.otherpackage, which is a sub-package of com, which is a sub-package of the global package.

While classes must be situated in it’s own file, with the name of the class being the name of the file, packages must be insde their own directories, the name of the package being the name of the folder.

This being said, for our com.otherpackage.mypackage package, we need the next directory structure:

  • com
    • otherpackage
      • mypackage

So if we had the com.Test and com.otherpackage.mypackage.Apple classes, just as we defined them above, we’d have the following directory and files structure:

  • com
    • otherpackage
      • mypackage
        • Apple.as
    • Test.as

Ofcourse, putting a class in the global package, we would not create any directory, the file should be placed right on the top folder of the classpath.

I mentioned the classpath, so i think it worth talking a bit about it.
The classpath is nothing else than the path where the flash compiler will look in order to find the classes you use in your project.
The directories of your packages are relative to this classpath.
The default classpath, is the root directory where you have your fla file (that is if you’re using Flash).
But you can change that to fit your needs.

That’s it about this part.
Any comments are welcome!

In the next part we’ll talk about inheritance and more about access specifiers.

DeliciousFacebookMySpaceStumbleUponDiggLinkedInRedditShare

admin Actionscript 3.0, Object Oriented Programming , ,

  1. May 6th, 2009 at 03:49 | #1

    Very interesting, nice simple explanation. I am looking forward to reading more from you.

  2. Dominic
    July 18th, 2009 at 06:18 | #2

    Thanks a lot, this helped with OOP.

  3. December 7th, 2009 at 11:46 | #3

    Glad that there are those that still spread the word of starting OOP, it’s a must know for all programmers.

  4. April 13th, 2010 at 03:23 | #4

    GREAT STUFF! cant wait for the next bit!

  5. October 8th, 2010 at 15:06 | #5

    Really Clear tutorial, nice one

  6. Rajesh
    May 31st, 2011 at 14:31 | #6

    Very nice!! usefull concept

  7. vinayaga
    June 21st, 2011 at 15:46 | #7

    its very easy to learn ,and very helpful for me

  1. May 4th, 2009 at 16:28 | #1
  2. July 30th, 2011 at 12:26 | #2