Objective-C is an object oriented programming language which is built upon the procedural language C, using OOP messaging based on Smalltalk. Although I had a basic idea on OOP through my University course, I recently purchased OOP Demystified (A self-teaching guide) to give me a deeper understanding of the concepts, which will help when I start looking at Objective-C in depth.
In this post I will share with you my understanding of the basic concepts of OOP. OOP is a rather deep subject and you may want to look into this further yourself as the book mentioned above also covers areas such as identifying and describing objects, modeling, and interfaces.
There are some terms related to OOP which you will probably hear of or read about as you delve further into Objective-C or other OOP languages yourself. Some of these are:
The main concept behind OOP is the defining of classes from which you can create objects. A class is the blueprint of an object, and each time you create an object, the class definition determines its structure. A class consists primarily of attributes (characteristics) that can store data, and methods (actions) that can perform operations. An object can also be called a class instance, and you will hear the terms class and instance variables used in Objective-C.
A class is created by putting related things together (encapsulation) from which an object can be created. The programmer will encapsulate attributes and methods that are related to a particular object.
Say for instance you were writing a program for a university. For this you would need to create objects for the students of the university. You would think of the various attributes that the student would have, and assign actions which would relate to a student. This is demonstrated in the diagram below:
Polymorphism is where you can have many class which have methods of the same name. You would do this when you have two or more classes whose methods perform similar tasks, but are completely different in the tasks they perform. You can give the methods in the two classes the same name, thereby alleviating the need to remember too many different method names.
Inheritance is where objects can use attributes and methods from other objects. This will cut down on the amount of code you will need to write as you simply send a message to another object to use its methods. Inheritance works using a parent/child hierarchy system, whereby the child class can access attributes and method of the parent class, however this cannot be done in the other direction.
Inheritance can be shown by going back to the university program. As mentioned earlier you would have a class for student. You could have another class for graduate students, which could inherit attributes and methods from the student class. The diagram below shows the classes for Student and GradStudent:
As you can see in the diagram above we are using polymorphism, in that both classes have methods called Write and Display. Also, there is no need to add attributes such as first name, last name etc. in the GradStudent class as these are inherited from the Student class.
There are three ways in which inheritance can be implemented: simple (shown above, multiple, and level inheritance). Multiple inheritance is where the inheritance involves multiple parents to a child:
Level inheritance is where a child inherits from a parent, and in turn becomes a parent itself. With level inheritance the maximum amount of levels that would be used is three, as things start to get complicated after that:
So how do we decide between multiple and level inheritance when the child inherits directly or indirectly from more than one parent? Here we use the “is/are a” method:
Hopefully this gives you a basic understanding of object oriented programming. I do however suggest you do some further reading in relation to areas such as identifying and describing objects.