Tuesday, June 28, 2011

Why Object Oriented Programming: Part 1

The most simple definition of OOP I can come up with is that it is a programming style that allows us as programmers to encapsulate both our logic and data in an object, thus making it more abstract. By doing so, data becomes easier to work with while also managing system complexity as your code base grows. This is a large topic, and I am not sure that my description here serves OOP justice in just how useful it is. Over the next few posts I intend to explain how this abstraction helps and hopefully you'll find it useful.

What is Object Oriented Programming(OOP)?

Well if you have programmed in languages like Java, Ruby, C++ you are likely already familiar with OOP. This style of programming uses objects that have attributes and methods for defining the functions that can be performed on that object. Pretty simple right? Ok, so lets define some of these terms.

An object as I'm sure you have guessed is a programmatic representation of a real world object. For example, lets say that you want to create a program to simulate a car. Well the car is the object. But knowing that the car is the object is just the beginning. How is a car defined? Well we can assume that a car has a speed, a rate of acceleration, wheels, and it carries passengers. These 'definitions' of what a car is are called the object's attributes. So lets see what this looks like in code.

PHP
Java

class Car {
   private $speed;
   private $acceleration;
   private $wheels;
   private $numOfPassengers;
}






Here you see that we created the Car object with the keyword class. Why? The simplest way to put this is that a class is template for defining an object. In the object body we have our attributes defined as private. If you aren't familiar with this type of scoping, suffice it to say that this keyword allows us to ensure that these attritubes are only capable of being edited by the objects functions. I will be explaining how to properly use scoping keywords in a later post.

So now we have our object defined, but it really doesn't do much. At the moment this code is useless, what we really need now is a way to instantiate a new object. To do this we will need to utilize a special type of method called a constructor, which will initialize the object attributes.



PHP
Java

class Car {
  private $speed;
  private $acceleration;
  private $wheels;
  private $numOfPassengers;

  public __construct($spd=0;$acc=0;$wheels=4;$numPassengers = 1){
    $this->speed = $spd;
    $this->acceleratio = $acc;
    $this->wheels = $wheels;
    $this->numOfPassengers = $numPassengers;
  }
}



You see in the code snippets above that the 2 languages provide a slightly different way of declaring constructors but they both accomplish the same task of initializing the object. You may be wondering what is with the this keyword. What this does is tell the system that the following variable belongs to this object. Why do this? Well you may notice that some of the parameters that the constructor takes are the same as the object's attributes. Using this to specify the variable being set makes sure that the right variable is being used. By default if 2 variables have the same name the one pertaining to the current scope is used. Lets see a quick example:


this.wheels = wheels; //sets the wheels attribute to the value passed to the constructor
wheels = wheels;      //would set the value passed to itself accomplishing nothing.


So now we have his object but it doens't do anything. In an object's most basic form it should have accessors and mutators for each of its attributes. What are these strange method types I speak of? Well they do exactly what they sound like. An accessor accesses the data held in an attribute and returns it. Whereas a mutator takes a parameter and sets the value of an attribute to the value of the parameter. It should be noted that a mutator method by definition changes the state of an object and can therefore take any number of parameters and alter any number of object attributes. I usually recommend that a method not alter any more than is absolutely necessary. So lets take a second to add these methods to the object.

PHP
Java


class Car {
  private $speed;
  private $acceleration;
  private $wheels;
  private $numOfPassengers;

  function __construct($spd=0;$acc=0;$wheels=4;$numPassengers = 1){
    $this->speed = $vel;
    $this->acceleration = $acc;
    $this->wheels = $wheels;
    $this->numOfPassengers = $numPassengers;
  }

  function getSpeed(){
    return $this->speed;
  }

  function setSpeed($spd=0){
    $this->speed = $spd;
  }

  function getAcceleration(){
    return $this->acceleration;
  }

  function setAccelration($accel=0){
    $this->acceleration = $accel;
  }

  function getNumOfWheels(){
    return $this->wheels;
  }

  function setNumOfWheels($wheels=4){
    $this->wheels = $wheels;
  }

  function getNumOfPassengers(){
    return $this->numOfPassengers;
  }

  function setNumOfPassengers($passengers=1){
    $this->numOfPassengers = $passengers;
  }
}


And there you have it! We have created an object that is ready to be used, now all thats left is to actually instantiate one and play around. To instantiate a new object you do the following.

PHP

$car = new Car(20,0,4,5);
$car->getSpeed(); //returns 20;


JAVA

Car car = new Car(10,5,6,5);
car.getAcceleration(); //returns 5;



Now that you know the basics on objects, go out there and create a few of your own!

No comments:

Post a Comment