Steve Jobs once said that computers are a blend of art and science. Programming can be viewed as a creative discipline. Just like designers have a toolset and a process to achieve an objective, object-oriented programming encapsulates both for programmers.
In traditional programming, the universe is viewed procedurally. “Things” occur in a linear order; this happens, then this, and if this is true, do this. That’s a perfectly acceptable organizational methodology, even though it doesn’t model the real world very well.
Think of the complexity in simulating a universe in a game like Grand Theft Auto. Things do not happen in a linear fashion; there is chaos in the physical universe. A butterfly flaps its wings and causes a hurricane… but how do you model this on a computer which is essentially no smarter than a “retarded cockroach” (according to physicist Michio Kaku)?
Object-oriented programming models a virtual universe based on philosophy inspired by Aristotle and Plato. When Greek philosphers started to observe and logically pick apart the way the world seemingly “works,” they discovered commonalities amongst the “objects” in our universe. So is the basis for organizing your code in OOP.
For instance, while sitting in the park, you might observe a chihuahua running around. You might notice another dog (a golden retriever) chewing on something. How would you model this “simple” universe on a computer procedurally?
var chihuahua; // create a chihuahuavar chihuahua_size = 1;chiuahua.run = function(){ Write("The Chihuahua runs.") } var great_dane; // create a Great Dane var great_dane_size = 50000; great_dane.chew = function(){ Write("The Great Dane chews something.") } chiuahua.run(); great_dane.chew();
But what if the chiuahua kicks the Great Dane’s ass and steals its hamburger chew-toy? And what if the Great Dane runs after the chiuahua? You’d have to go in and write new subroutines for each behaviour for each dog. That would work, but it would quickly become confusing and time-consuming if 2,000 dogs escape from the kennel and run around in our virtual-park.
Like Aristotle, in OOP we would recognize the similarities between the dogs and create a dog class. Dogs do stuff, and dogs have properties such as size, color, furriness, etc. Let’s see how this could be setup:
class Dog { // We create a base class with all the common properties and methods of dogsvar size;var color;var breed;function run(breed){ Write("The" + breed + " runs.") } function chew(breed){ Write("The" + breed + " chews something.") } } class Chiuahua extends Dog { // We create a new type of dog that inherits the properties of all dogs size = 1; color = brown; breed = "Chiuahua"; }
We could then create more dogs that extend the dog class, and set them loose in our park by instantiating them:
Chiuahua beavis = new Chiuahua();Great_Dane princess = new Great_Dane();princess.chew(); beavis.chew(); princess.run(); beavis.run();
See you next week.
~Jason




