To start off Part 2, I'd like to take just a brief moment to explain why I picked this title (Object-Oriented Programming for the Masses) for my article. I believe that Cocoa is the easiest and most logical method of GUI-based, object-oriented application programming available in a mainstream operating system. While many programming experts have welcomed Cocoa with open arms because of its RAD (Rapid Application Development) qualities and powerful foundation classes, I see its major strength as being a window of opportunity for regular "Joe" users with some technical computer skills to "make the leap" and become software producers, not just software consumers. And no, I'm not just talking through my hat. The amount of innovation I'm seeing right now in the Mac OS X software community is staggering, and much of it is coming from people who have slowly grown into the role of being a software developer, not people with 20+ years experience in programming and boasting several degrees in computer science. Though the fruits of my own labor have yet to be shown to the public, slowly but surely I find myself growing into the role of software developer as well. And I can easily claim that Cocoa is the reason, the only reason, that this has occurred.
Now if you'll recall, last time I introduced you to a few of the basics of Objective-C. In particular, the fact that every Objective-C object belongs to a class. Also, I stated that the concept of encapsulation is important in object-oriented programming -- that is, the implementation of an object is hidden behind its interface. That's where I left off previously, and that's where we'll pick up the trail this time.
In spite of all that was presented in Part 1, I missed showing you one very important thing: exactly how to define a class in code. This is because it's best to know a bit more about Cocoa before you can understand how that process works. So I'll use this opportunity to introduce a few of the basics of the Cocoa framework, as well continue on with our exploration of Objective-C.
Remember, every Objective-C class features at least one of these attributes: class methods (for the class objects), instance methods (for the regular object instances), and instance variables. So how exactly is a class definition created? Objective-C typically follows the grand C tradition of splitting up groups of code (in this case, objects) into two files: a header file and the actual code file. In Objective-C, this usually translates as: the interface file and the implementation file. For example, a
MyPerson class would be defined using two files:
MyPerson.m. The .h is the standard C header extension, and the .m stands for "Objective-C code module".