Introduction to Object-Oriented Programming–1 (Classes)

Although “Object-Oriented Programming” combination of these words is scary for beginning programmers, but after beginning the OOP it turns out that all process is not so hard actually. I think the difficult part for beginner is always adaptation process. For making this process little easier I will make a short introduction to object-oriented programming. In this and the next few sections I will explain the basic terms and they part of work in object oriented programming.

Classes and Objects

Classes are code snippets which do simple tasks for us. To do a specific job, sometimes one, sometimes several different classes at the same time can be used. For using these classes we have to create an Object of a class.

For example, let say there is an image animation that you use in every project you’re working on, which is working with 40 lines of codes. Normally (when writing code in timeline), you have to copy and paste these 40 lines of code every time you want to use the animation. Rather than that you can write bit of more code and turn that animation code as a single ActionScript file. In this case has been developed a Class. You can use the animation of this class file in Timeline or in another Class structure. But whenever you use the Class file you have created you are actually creating and using Object. Which means an Object is instance of a class file.

Let us say that the name given to the class file is ImageAnimationClass, to use this animation we have to create an Object belonging to this class. So;

var anim: ImageAnimationClass = new ImageAnimationClass ();

The code above is an object that is a reflection of the class. In short, objects are an abstract reflection of. Now let us go into the technical part. When creating a class, you need to follow the simple rules.

First I will start with ActionScript File. ActionScript class files can be created by a simple text editor such as Notepad. The only thing you need to do is to register as *.as extension. But it is logical to use programs such as Flash Professional, Flex Builder or Flash Developer. If you are using Flash, if you are opened your Flash you can select Actionscript File from Welcome Window or you can select from menu File > New > Actionscript File. Soon as you select there a blank file ActionScript file will open.

First rule is name of file and name of class have to be same. For example if name of your class is ImageAnimationClass name of file must be ImageAnimationClass.as. Normally class names begin with capital letter. When there is more than a word than words become a single word sequence without any space and again each word is begin with capital letter. Generally single ActionScript file has a single class. This is not a rule of course but in fact this is the most common method. The reason is common is not because it is a classical course, because it is functional.

PS: In a single class file can be too many class structures, but only one of them will be accessible outside the class file. The class which can be access must be included in package statement

Writing a Class

When you have to write a Class you must use the class word. Then the name of the class comes.

After the class name there will be curly braces ({}).  Functions, variables and rest of the code which belongs the class will take place between curly braces ({}).The following example has a basic class structure, when this class works, trace () method will give an output.

SampleClass.as

package {
        public class SampleClass {
                public function SampleClass() {
                        trace("Sample class did work");
                }
        }
}

In this example SampleClass() method is a constructor method of the class. When class structure is loaded and starts this method running automatically. So if there is a snippet of code or functions within class structure that have to work, all you have to do place in to constructor method.

One more thing; You can think of the Function as is equal to the original concept of the concept of the Method.
A function prepared within Class structure is called method. A variable prepared within Class structure is called property. This is basic information for beginning, in other articles I will explain differences with much more detail. The following example is show how property and methods works.

SampleClass.as

package {
        public class SampleClass {
                public var testProperty:String="Test property did work";

                public function SampleClass():void {
                        trace("Sample class did work");
                }

                public function testMethod():void {
                        trace("Test method did work");
                }
        }
}

Testing this code is also simple, all you have to do is import the class and create new object, than call the method and trace out the property;

import SampleClass;
var testObject: SampleClass = new SampleClass ();
testObject.testMethod();
trace(testObject.testProperty);

The output will be as following;
Sample class did work
Test method did work
Test property did work

Up Shot

In next articles I will cover the packages and class attributes.

Take care
Engin!

Leave a Reply