Wednesday, September 10, 2014

Object Orientation


Here's the audio version of this blog post:



These are objects
What is an object?  In real life, a lot of things are objects.  Plants, bicycles, cars, pets, bananas, thoughts, really anything!  Even people could be considered objects (bear with me here).  The question "what is an object" raises some philosophical issues, but it is clear that objects play a large role in our daily lives.

Because objects are so pervasive in our perception of the world, we can say we experience life in an object-oriented way.  For example, take the sentence "I'm going to the store".  Here, "I" refers to the person object that is going to the store.  The store could be interpreted as a destination object, which implies that there is also an origin object.  The subject of our example may plan on driving his car object to the store, or riding a bike object.  At the store, a person can buy food objects.

This example again raises the question, What is an object?
Let's define some criteria for object-ness:

  • An object must have characteristics
    • Tangible characteristics:
      • Color, size, weight, location
    • Non-tangible characteristics:
      • Emotions, personality traits, powered on, powered off
  • An object must have actions
    • Things that it can do or things that can be done to it
      • Run, walk, eat, drive
      • Power on, power off
      • Paint (change color)
      • Grow
  • An object must be identifiable
    • It must be a distinct entity
      • I can have two pens of the same model, but I still have two different pens
Notice how we never specified that an object must physically exist.  Our criteria for an object could include thoughts, conversations or mathematical formulas.

Because objects are so omnipresent in life and the human experience, things we create will reflect this object-oriented way of thinking.  Humans can easily think of problems in terms of objects, so we created many software tools to describe and interact with objects.  Programs that simulate icebergs melting or control aircraft scheduling patterns can be developed in an object-oriented way.

Not how it is
So, this means when we write most programs, it is easiest to write them in an object-oriented way.  This results in minimal effort on the programmer's part.  Well designed object-oriented programs read more like a description of the real world than arcane source code.









Alright, let's see some code.

class Pig:
    #definition for Pig object
    def __init__(self, weight, age, name):
        #a pig has weight (lbs), age (years) and a name
        
        #assigning parameters to pig object
        self.weight = weight
        self.age = age
        self.name = name
    
    def oink(self):
        print "Oink!"
        
    def sayHello(self):
        print "Hello, my name is " + self.name + "."

#make a new pig named Spider pig that weighs 40 lbs.
#and is 3 years old
SpiderPig = Pig(40, 3, "Spider Pig")

#make spider pig introduce himself
SpiderPig.sayHello()

#make spider pig oink
SpiderPig.oink()

Here's a link to an editable and runnable version of that example.

The programming language I used to write that example is called Python (named after Monty Python's Flying Circus).  If you want a more in-depth introduction, check out my blog posts Python Intro and Python Intro Part Deux.  I also wrote an introduction to Java, another object oriented programming language.

No comments:

Post a Comment