What is OOP: Object Oriented Programming.

Lets begin with a simple project:

There are two points with coordinates (3,6) and (6,1); What are the distance of points to (0,0) ?

We can approach this as a problem of 4 variables or we can approach this as a problem of 2 points objects. We can say that, the later is somewhat Object oriented programming.

Approach one:

trace(Math.sqrt(3*3+6*6));
trace(Math.sqrt(6*6+1*1));

That code works and is very efficient.
However, the coordinates are  "hardcoded" in the script. When we face similar project with different coordinate values of point 1 , say it is (3,5), we need to check every line of the scripts and change it. It would be a hard work. Maybe at last, we know that, it is the 6 in the first line that should be changed to 5. Not the 6 in the second line.

This is bad for "code re-use".

Lets modify our scripting habit by function:

x1=3;
y1=6;
x2=1;
y2=6;
function getDistance(x,y){
return Math.sqrt(x*x+y*y);
}
distance1=getDistance(x1.y1);
distance2=getDistance(x2,y2);
trace(distance1);
trace(distance2);

This is improvement in code re-use. If the coordinate value of point 1 is changed, then we just change y1=6 to y1=5; Here we have 6 variables. x1 means x value of point 1,distance 1 means distance of point 1;.....etc. However, if the project turn complicated, the variables definition will be a long list and confusing.

Now we introduce the objects. The Object oriented programming - OOP.

Point = function (x, y) {
this.x = x;
this.y = y;
this.getDistance=Math.sqrt(this.x*this.x+this.y*this.y);
};
point1=new Point(3,6);
point2=new Point(6,1);
trace(point1.distance);
trace(point2.distance);

Here we group common "properties" into an "object". Point is a Class. point1 and point2 are instances of Point Class.

We say point1.x , point2.x  instead of x1,x2 .

We say point1.distance and point2.distance instead of distance1, distance 2.

In our brain, this is a project about two point-objects  instead of 6 variables. In another words, we break up our project into two parts. One job is writing codes for defining the Point Class, giving this class many methods. The other job is handling of these two Point objects.

Thus, it is a project about two points objects not a project about 6 variables.

Lest take a look at the example in the real world: the administration of employer in a company.

Every employer has his photo and address sheet. I have many employers. It is bad to pile these data up all on the desk into a mess.

The approach one:

We might have one album collecting all the photos of our employer and have one address book to register all the address of them. We may have one paper sheet storing the diferrent salary of them. Well, that is a good way to handle information.

Another approach:

We may prepare many file sheets. We plan to put the photo, address and the salary of each employer into a file sheets.  This file template is a "Class". When we have new employer, we create a new file. This is an "instance" of that class. All instances have properties such as "photo","address",and "salary". Of-course, the "values" of photo, address and salary are different among those instances.

Properties is a basic feature of "Object"; We will discuss later about "Method" in object. It is also different from the concepts of "function";