Thursday, February 23, 2012

What is Object Oriented programming?

Object Oriented programming started back when C++ came out.

The three tenents of Object Oriented programming are:

Inheritance
Encapsualtion
Polymorphism

Let's tackle these one at a time....

Inheritance

Let's say you have a class called Vehicle and defined as:

public class Vehicle
{
   protected int wheels;
   protected int seats;
   protected int speed;
   protected float fuel;

   public Vehicle( int Wheels, int Seats, int Speed, float Fuel )
   {
      wheels = Wheels;
      seats = Seats;
      speed = Speed;
     fuel = Fuel;
   }
   public virtual void Drive();
}

Now let's say that you need a class called Car and another called Motorcycle:

public class Car : Vehicle
{
   public Car( int Seats,  int Speed, float Fuel )
   {
      base.wheels = 4;
      base.seats = Seats;
      base.speed = Speed;
      base.fuel = Fuel;
   }
 
  public virtual void Drive()
  {
     // Drive the car
  }
}

public class Motorcycle : Vehicle
{
   public Motorcycle( int Seats, int Speed, float Fuel )
   {
       base.wheels = 2;
       base.seats = Seats;
       base.speed = Speed;
       base.fuel = Fuel;
   }

   public virtual void Drive()
   {
      // Drive the Motorcycle
   }
}

Notice that the Car and the Motorcycle have no local storage, only the Vehicle has data members.

Also notice the : Vehicle after Car and Motorcycle this indicates that Car and Motorcycle inherit from Vehicle.

Encapsualation

Now let's say that you create a Radio class for entertainment.

public class Radio
{
   private float station;
   private float volume;
   private bool on;

   public void Tune( float Station )
   {
      station = Station;
   }

   public void VolumeUp( float amount )
   {
     volume += amount;
   }

   public void VolumeDown( float amount )
   {
      volume -= amount;
   }

   public void ToggleOnOff()
   {
      on = !on;
   }
}

Now you'd like to add the radio to the Car but you don't think it belongs in Vehicle.

public class Car
{
    private Radio radio = new Radio();
   
    public void Tune( float Station )
    {
        radio.Tune( Station );
    }

    public void VolumeUp( float amount )
    {
       radio.VolumeUp( amount );
    }

    public void RadioOnOff()
    {
       radio.ToggleOnOff();
    }

    // the rest of Car is as before
}

You could say that the Radio is encapsualated within the Car.

Polymorphism

Let's say that you've created a Shape class as:

public class Shape
{
    public virtual float Area();
    public virtual void Draw();
}

Now you want to create some specific shapes:

public class Circle : Shape
{
    private int diameter;

    public Circle( int Diameter )
    {
       diameter = Diameter;
    }

    public virtual float Area()
    {
       // compute the Area of a circle
      return 0.0; // temp value until we actually compute the area
    }

    public virtual void Draw()
    {
       // draw the Circle
    }
}

public class Square : Shape
{
   int side;

   public Square( int Side )
   {
      side = Side;
   }

   public virtual float Area()
   {
      return side * side;
   }

   public virtual void Draw()
   {
      // draw the Square
   }
}

Next you want to randomly create either a Circle or a Square:

public class Factory
{
    public Shape Create()
    {
        Shape shape = new Shape();
        Random r = new Random( DateTime.Now.Second );
       
        switch( r.Next( 0, 1 )
        {
           case 0:
              shape = new Circle( r.Next( 10 ) );
              break;
           case 1:
              shape = new Square( r.Next( 10 ) );
              break;
        }

        return shape;
    }
}

Next let's say that you want to create a loop that draw circles and squares without knowing which one you have:
public DrawShapes()
{
  Factory f = new Factory();
  for( int i = 0; i < 10; ++i )
  {
     Shape s = f.Create();
     s.Draw();
  }
}

What you just saw was an example of Polymorphism.

Now that you understand the basics of Object Oriented programming, how do you go about designing your own classes?

Classes are generally nouns or things. Notice our example classes were Vehicle, Car, Motorcyle, Shape, Circle and Square.
Data within classes are generally either private or protected.
Methods within Classes are generally verbs or actions. Notice that our methods were Drive, Area, Draw, etc.
Methods are generally public but if you need a private helper method that's fine too.

And that's about all you need to know about Object Oriented programming.

No comments:

Post a Comment