I have moved my blog to Wordpress at theunixgeek.wordpress.com. I will still be checking back periodically on this one as well, though. 19 April 2009


Merging Mkdir and Cd | 280 Slides Interview | I Switched to KDE 4

clickable portals

Friday, November 2, 2007

Understanding Object-Oriented Philosophy

NOTE: No full program examples will be given, only code snippets. 

I guess you could look at object-oriented programs as a sort of philosophy. Sure, it may look cruel and uninviting at first, but after a while, it begins to seem quite useful. Let me explain (examples given in Objective-C).

Object-oriented programs are based off classes. A class is at the top of the chain. Let's take the example of the class "Human." Classes are further divided into subclasses, which eventually will have objects. Objects inherit all the traits from their respective superclasses (that is, instance variables and methods).

Ok. This is starting to get confusing. Let me break it down.

A superclass is a class above another one. A subclass is a class below another one. An object is specific to a class and holds operations (called "methods") and traits (instance variables). Now, I'm assuming that you already have some C programming experience - enough to know what a variable is and what #include does.

Now reread the above paragraph (Object-oriented programs are based...)  and you'll understand it better. 

Remember to use #import instead of #include. Sure, you can use #include, but #import has little secrets that make it work better.

Going back to classes, allow me to continue. Each class holds instance variables, which are used when an instance of the object is created. If you want you could create what's called an "abstract" class, which doesn't really do much itself but serves for data storage of methods and instance variables? Now, what is an instance variable? These are also inherited (Note: inherited code is not duplicated; it is read directly from the superclass). In the Human class, some variables could be eye, nose, mouth, ears, etc. They are shared by most humans. In the Male and Female, these instance variables would be inherited and others would also be created. You can think of an instance for human as a baby being born. Before an object can be used, memory must be assigned to its instance. Here's how you'd create an instance of the object MaleChild, for example:

MaleChild *baby; //create an instance 
baby = [MaleChild alloc]; //allocate memory to the instance
baby = [baby init];

You may have noticed the [brackets] in the code. Get used to them. Objective-C is the land of brackets. In Java, you don't need to get used to them. I don't think they're used in C++ either. Anyway, what do these represent? They're used for what's called "object messaging," where a message is used to get an object to apply a method. One action babies do that adults don't is crawling. If we wanted the new instance of a male child to crawl, we could say [baby crawl]; If we wanted to specify how many steps, we could set up the method to allow arguments. If the baby were to crawl, say, five steps, we could possibly say 

int numberOfSteps = 5;
[baby crawl:numberOfSteps]; 

If you wanted, you could also nest messages, such as

[baby crawl:[numberOfSteps getSteps:numberOfSteps]];

Note that numberOfSteps is used as both a method name an variable name; this is allowed in Objective-C.

That's it for this little introduction to Object-Oriented philosophy, which usually seems hard at the beginning, but can actually help. You may notice yourself writing longer code in the beginning with object-oriented languages, but don't worry. When you start writing longer code, using instances of objects will be quite helpful.