Object Oriented Programming Concepts

Previous Topic Previous Next Topic Next
Xoc Software

Other Xoc managed sites:
From a posting I made on http://www.webmasterworld.com.

Object oriented programming (OOP) takes a different mindset than procedural programming, or even event-driven programming. I think it really took me six months after I was exposed to the features that I really got the philosophy of object oriented programming.

One of the problems with OOP is that the terminology is intimidating. In the Visual Basic courses I teach, I like the use an analogy:

Library: Box of cookie cutters
Class: Cookie cutter 
Object: Cookie 
Memory: Cookie dough 
Instantiate: Cut a cookie with the cutter 
Object Variable: a single minded 3-year old;
  i.e. a cookie pointer..."look, daddy, a cookie",
  pointing at the cookie, and can only point at one cookie at a time. 

Now that is Visual Basic terminology, but other languages have similar terms. This analogy holds up pretty well and helped me understand how it all works.

The thing that took a while form me to get, is that I started by treating the objects as somewhat sophisticated structs. But as time went on, I realized that any time you want to pull information from a property to do a calculation on it, should probably be turned into a method.

Another analogy that helped:

Object: Noun (specifically a direct object)
Property: Adjective 
Method: Transitive Verb 

For example:

Class: Book
Object: chris pointing at a specific Book
Property: cover is black
Property: title is Emphyrio
Property: author is Jack Vance
Method: Open
Method: Read(Page 12)
Method: Close

As in

Dim chris As Book 
Set chris = New Book 
chris.cover = vbBlack 
chris.title = "Emphyrio" 
chris.author = "Jack Vance" 
Call chris.Open() 
Call chris.Read(Page:=12) 
Call chris.Close() 

which would be valid VB syntax. The Read method basically is a command that tells VB "You read the book pointed at by my 3 year old named chris (and you'll want page 12)"


If chris.Title = "Emphyrio" Then 

Which asks if the book that chris is pointing has a title of Emphyrio.