My struggles in understanding and learning about Object Oriented design, and the tools and knowledge I've taken from them.

Thursday, October 9, 2008

Object Oriented Programming (Part 1)

As I've mentioned, Object Oriented programming is a paradigm, or a way of thinking. This is in comparison to procedural oriented programming, where you have a bunch of variables and functions (methods), and they may or may not interact with one another. Procedural oriented programming is quite chaotic, because there's not necessarily any organization or modularity (big pieces broken into smaller pieces).
I took a lot of philosophy courses in college, and one of my favorite philosophical quotes is by Rene DesCartes (you know, the "I think, therefore I am" dude?). The quote is "Divide each difficulty into as many parts as is feasible and necessary to resolve it." I think this quote is exactly what Object Oriented design achieves. Things that are logically different are stored in a different place.

For example, a human is different than a dog. Behavior, or actions, of a human is different than that of a dog. The properties of a human are different than a dog. To get more specific, a dog walks on 4 legs, where a human walks on 2. A dog eats without using its hands, where a human eats using his or her hands. A dog has a tail, and a human does not. A human goes to work, can buy a house, can drive a car, etc.

So, to lay it out in more general terms, object oriented programming is characterized by its implementation of CLASSES.

Think of a CLASS like a noun. A noun is something. For example, a human is a noun, a dog is a noun, a car is a noun, and a house is a noun. All of these "nouns" can be represented by a CLASS.

CLASSES are INSTANTIATED. An INSTANTIATED CLASS is called an OBJECT, hence the phrase Object Oriented Programming.

Think of an OBJECT as a more specific CLASS. They're both nouns. One of the examples I see a lot is comparing Dog and Lassie. The CLASS is Dog, and the OBJECT is Lassie.

For instance, this could be represented with the following code:

class Dog
{
    //Properties and methods go in here
}

Dog Lassie = new Dog();



Notice in the above example that I put a comment in the Dog class to indicate that PROPERTIES and METHODS go inside the class. I did this because a CLASS is made up of PROPERTIES and METHODS.

METHODS
Think of a METHOD as a verb. METHODS are things that an object can do. For instance, a Human OBJECT can generally Walk(). Human beings, also can generally Talk(). The verbs Walk and Talk represent the METHODS that the class Human can implement.

PROPERTIES
The other side of the coin is PROPERTIES. A PROPERTY is generally a characteristic (please note that a property can be more than just a characteristic). For instance, a Human can have hair and hair color. A Human can also have some number of fingers, ranging from 0 to 10, with a default value of 10.

So, before I start explaining too many properties and methods a human can have, I'll put a code example that describes the above examples:

class Human
{
    //Private members of the class
    private string _hairColor;
    private int _numberOfFingers;

    //Properties of Human Class
    public string HairColor
    {
      get { return _hairColor; }
      set { _hairColor = value; }
    }

    public int NumberOfFingers
    {
       get { return _numberOfFingers; }
       set { _numberOfFingers = value; }
    }

    //Methods of Human Class
    public void Talk(string words)
    {
      Console.WriteLine(words);
    }

    public void Walk()
    {
      Console.WriteLine("Human is walking, now");
    }
}

//Instantiation
Human Tim = new Human(); //Class Human is instantiated as "Tim"
Tim.HairColor = "Light Brown";
Tim.NumberOfFingers = 10;
Tim.Walk();
Tim.Talk("Hey man, I'm instantiated");


The above is a very simple example, and please don't get too bogged down in the syntax or implementation of the properties I used. I'll go on to explain these concepts in later posts.

No comments:

Followers

Search This Blog

Powered by Blogger.