Are objects instantiated in @interface?


Another question about p112. The instance chain is as follows:
employee --> person -->NSObject

I instantiated the class Employee in main.m via

But where does Person and NSObject get instantiated? Does it occur in the @interface declaration?

@interface Employee : Person from Employ.h

@interface Person : NSObject from Person.h


The .h files are indeed where you specify the inheritance chain, but the other classes aren’t instantiated per se.

Instantiation is the process during which a class is transformed from definition into an actual object. The Employee class is instantiated into an object; the methods and attributes defined in Person and NSObject come along for the ride. (I guess you could say that all 3 classes are instantiated simultaneously into one object, but I’m not confident that’s a technically accurate statement.)


It seems that since the Person class has properties (heightInMeters, weightInKilos) and those properties are accessed (albeit through inheritance), that the Person class is indeed an object (and was instantiated - how, I’m not sure).

If Person were a class only, accessed by the employ object then it seems that Person shouldn’t have properties (instance variables). I think there are classes whose methods are used without instantiation (don’t remember the name though).


My understanding of the inheritance isa chain is that the supers aren’t instantiated when you init a subclass.

Throw a breakpoint into a project and you can see how they add up.

person isa Employee isa Person isa NSObject. Only the person instance of Employee has an address.

The assets and employeeID are just in the Employee class.


Interesting, I get a different result:


This is an incredibly difficult topic to address without going very deep into the functionality of the Objective-C runtime.

The short answer: When you instantiate an object, only that object is actually created.

When we use the word ‘instantiate’, we mean that memory is being allocated by the system to store an object, such as an NSString instance. An object is instantiated when its class is sent the +alloc message. The header file is nothing more than a place to describe what it means to be an instance of a given class. That is, any time a new object is instantiated, it will contain certain instance variables, such as ints, floats, and pointers (to other objects) as declared in its class’ header file. That is, you could define a class called Dog, give your Employee a pointer to one in Employee.h, but when your application is running, there are no actual instances of dog in your application until code such as hisDog = [[Dog alloc] init]; is executed.

Brianpx, the reason that you see additional isa pointers beyond NSObject in the hierarchy in your picture is due to the deep rabbit-hole that is the Objective-C runtime. Specifically, that each class (such as NSString) is itself an object, and that each class is really a pair of two classes: a Class (with a capital C), and its meta-class. If this information has left your brain intact (mine was in pieces the first time I learned it), then there’s lots of additional reading on the subject here and here and here. Otherwise, I recommend saving these links until you’ve got a bit more Objective-C under your belt.