Monday, August 18, 2014

Object Oriented Programming - what exactly is it?

Similar to how we have different writing styles in the English Language, in computer technology we have different types of programming models or ways to write a program. As writing styles can be technical, business oriented, or conversational, programming models can be object oriented, functional, procedural, structured, or event driven.

Here I would like to stress the difference between programming models and programming languages. Object Oriented Programming is a type of programming model. It's a format or model for writing code. Java, C, C++, Php, Python, C# are programming languages that are based on the object oriented programming model.

So how does object oriented programming work? It's all about observing the physical world around you. If you started to look around your room, you would find clothes, books, furniture. If you looked around the house, you would find plates, tables, and more furniture. The easiest way to describe all of these "things" is to say your room and house contains objects. What we've done is gone from something very specific and made a generalization. We took specific things and generalized them all as objects.

Now as we observe objects we see they have 2 qualities and characteristics. One is that they have state and the other is they have behavior that is related to that state. Say you had a basketball. A few possible states it could be in is it is either at rest or in motion. A few examples of behavior related to the basketball is you could be holding it, dribbling it, passing it, or shooting it.

Let's take the help of the English language and grammar to help us better understand the concept of object oriented programming. In  the English language when we construct sentences we have nouns, adjectives, and verbs. Take for example the following short story about a boy and a ball.

The boy has a ball.
His name is Billy.
The color of the ball is blue.
The boy dribbles the ball.
The boy kicks the ball.

There are 2 objects of interest in this story. They boy and the ball. Each of these objects has a state and related behavior. We can identify the state by looking at the nouns and adjectives in the sentences. We can identify the behavior by looking at the actions in the sentences.

For the object known as the Boy, we could say:
The Boy's state could be identified by his name and whether he has an object called a ball.
The Boy's behavior could be identified by what he is doing with the ball object, either kicking it or dribbling it.

For the object known as the Ball, we could say:
The Ball's state could be identified by its color and its state of motion.
The Ball's behavior could be identified as rolling if it is kicked or bouncing if it is being dribbled.

Now say we wanted to make a small Game Application of a Boy playing with a Ball. We could see from above that at the very least we would need the 2 objects mentioned above. These 2 objects would interact with one another through the behavior they expose. Since this behavior is  related to the internal state of the object, we could change the state of the objects through the behavior. For example:

If the boy uses his behavior of kicking the ball, the state of the ball's motion would be changed using the rolling behavior of the ball.

If the boy uses his behavior of dribbling the ball, the state of the ball's motion would be changed using the bouncing behavior of the ball.

In a real video game it would be more complicated as the actions/behavior of kicking and dribbling would not only change the internal state of the objects but also visually describe what is happening graphically at the standard output, i.e. computer screen. This visual depiction of graphics is also part of the change of state and behavior of the Boy and Ball objects.