Tag Archives: OOP

Objective-C: Inheritance

If you read my last post on Objective-C: Classes and Objects, you would have seen mention of the Car class inheriting from the NSObject class. You may also have encountered a description of OOP inheritance in a previous post on Object Oriented Programming, and be wondering how this works in relation to Objective-C.

In a class interface file you will see something like:
@interface MyClassA : NSObject
This is basically Objective-C inheritance at work. Within the methods you have set up for your MyClassA class, which is also known as the subclass or child class, you can use instance variables and methods of the NSObject class, which is the superclass or parent class of MyClassA. NSObject is the root class in Objective-C, meaning it is at the top of the hierarchy, so has no classes above it.

Now you can set up a new class called:
@interface MyClassB : MyClassA
which will inherit the instance variables and methods from both MyClassA and NSObject, as MyClassA already inherits from NSObject. Each instance of a class receives its own instance variables, even ones inherited from parent classes.

I hope this helps to clarify inheritance and how it is implemented in Objective-C.

Objective-C: Classes and Objects

In his book Programming in Objective-C 2.0, Stephen Kochan uses a very good way of describing classes and objects: An object is a “thing”. Think about object-oriented programming as a thing and something you want to do with that thing. He then goes on to describe the concept of creating a class using a “car”.

So, we start with the class name which is Car (class names in Objective-C should begin with a capital letter).

A class can be made up of attributes (instance variables), and methods (things the class can do). So for a car, the attributes could be:

  • Exterior colour
  • Interior colour
  • Roof type
  • Engine Size
  • …and many other attributes

Some of the instance methods for a car could be:

  • Drive
  • Fill up
  • Service
  • Wash
  • …and many other methods

A class method for a car could be:

  • How many types of car a manufacture makes

So what is the difference between an instance and class method? An instance method applies to each individual car object (class instance), whereas a class method performs some function on the class itself, such as creating a new instance of a class.

To set up the Car class you would start with the interface section, where you declare the attributes and methods:
Car interface
First we have the @interface declaration, followed by the class name Car and NSObject which is the parent class of this class, meaning that the Car class inherits attributes and methods of the NSObject class.

Next, inside the curly braces {} we declare the instance variables. Here I have simply chosen a sample of possible attributes for this class.

Then, after the attribute declarations and just before the @end statement we declare the instance methods.

We now implement the methods for the class in the implementation section:
Car implementation
I haven’t gone into detail here, however you can see that inside the @implementation and @end we have our four instance methods ready for their respective implementation code to be inserted.

Once you have the interface and implementation of the class set up you are ready to create car objects (class instances), which is done as follows:
Car object
Here I have inserted the code inside main.h as I created a basic command line tool project, however it will be inside a specific .m file inside an iOS project.

First we create and initialise a new car object by saying we are creating a new Car called myCar and sending a message – alloc (which with we have inherited from the NSObject class) to the Car class, then initialising it.

We can now send messages to our myCar object to drive, service etc…, and finally when finished with the object we release it from memory (which I will discuss at a later date).

We can also create new car objects that use the same methods as myCar, although are individual to each car object (as mentioned above). This is one of the key concepts of object oriented programming.

I have not gone into great detail here as my intention was to give an overview of classes and objects in relation to Objective-C.

OOP Encapsulation

In my previous post on the topic of Object Oriented Programming, I briefly touched on encapsulation as “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.”

This was my understanding from reading a previous text, however on reading Stephen Kochan’s Programming in Objective-C, he has made the subject of encapsulation easier to understand in relation the Objective-C language.

“What if you wanted to access your instance variables from someplace else – for example, from inside your main routine? You can’t do that directly because they are hidden. The fact that they are hidden from you is a key concept called data encapsulation…”.

He goes on to say “You can access your instance variables in a clean way by writing special methods to set and retrieve their values”, and describes the theory of setters and getters, and how to set them up manually. As I have already worked through some chapters of an iOS programming book, I have already come across setters and getters, however did not realise their relevance to the subject of encapsulation.

With regards to setting the setters and getters manually, Steve does this so that you learn the theory, as later in the book he will go on to describe the @property and @synthesize statements for setting up the setters and getters easier.

Object Oriented Programming (OOP)

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:

  • Encapsulation
  • Inheritance
  • Polymorphism

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:

Class disgram

Polymorphism

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

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:

Inheritance Diagram
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:

Multiple inheritance
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:

Level inheritance
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:

Level Inheritance

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.