Sunday, December 17, 2006

Organizing the Objects

Programming Evolution

Inside the programming world many changes and evolutions have happened.

People initially believed only in instructing (one instruction at a time) a computer and getting responses from it, later they thought that a computer can take or execute set of instructions at a time and then provide a meaningful result. Then they thought if the instructions can be reused as a set they can as well be saved or defined so that they can be executed whenever needed. This concept of executing set of instructions led to a concept called methods or behaviors (in oops). Now instead of executing instruction by instruction we can call or execute a method which will in turn execute all the instructions defined within it, not only that a set of methods can be called from a defined method so that all the methods get executed and provide the output.

Now, how do these methods communicate or share their respective outputs or data.

Variables – variables play an important role in saving and sharing data within or across methods. Variable scoped to various levels like local, global etc. A local variable can be used only within a method as it’s defined within a method. Here, understanding the concept of blocks (chunks of code) is very important.

{
int k=10;
{
int x=2,y=3;
int z = add(x,y); //where add is a method
}
print(k); //k is avaliable
print(x,y,z); //Error x,y,z are not available they are already forgotten
}

In the above piece of code, the code between blue braces is a block and there is an inner block with green braces.

The variables defined in green braces inner block are not available in the outer block as they are defined within the block itself, hence their scope is within the green braces block and they cannot be accessed outside it. So, it throws a error something like “undefined variables” when u try to print them or use them outside the green block (ex: - xyz in blue block).

Variables can be used to store data temporarily, for example when we add 1 and 2 the result can be stored in a variable called ‘z’ and can be used in multiple places within the code to print or to do another operation example multiplying z with 4. z x 4

z = 1 + 2;
(I assume that you understand the operators +, * etc., Anyways operators perform operations like addition, multiplication etc., and here = is an assignment operator where a result is assigned to z which means that z holds the result of 1+2 which is 3)


I hope now you understood what variables and methods.

The concept or methods, variables and operators were enough to attain any kind of complex software available today. But the rate of programming and designing and understanding such conventional code was difficult and hard to maintain, methods variables would lie all over and there was no way in which they can be encapsulated or classified.

New rules, concepts and methodologies were been discovered to make programming more reusable and easily understandable. The concept of structures and classes were introduced by all the new programming languages. We can create our own definition of Data type or structures and Even define classes which can hold a set of variables or methods and create objects out of a class definition when ever required.

The above evolution led to Object Oriented Programming.


Object Oriented Programming

First of all let us understand what objects are in our every day life. We can call any substance or material or a cell phone, a car it might be any such thing which has some features, some behaviors or some activities which we can do upon it.

For example, let us take a car. We can consider the whole of a car as an object which is built using multiple objects like tires, steering wheel, bumper etc., and each object has different behavior, shape, size color etc., each object has its own responsibilities which help the car to function.

Most important point here is the interaction or communication within the objects. If the objects are not organized or built property they can perform or work together.

Now let us move to the virtual world of programming.

In “Object Oriented Programming” a new concept of “class” was been introduced which is nothing but a simple outermost block over all the methods and variables.

Ex:-

class MyClass {
variable1; //variable class scope
method1() {
variable2; //inner variable method scope
{//inner block
variable3; // inner block scope
}
}
}