Reputation: 919
What is the difference between importing a header file of a class and inheriting a class?
Person.h:
#import <Foundation/Foundation.h>
@interface Person:NSObject
{float fromPerson;}
-(void)inPerson;
@end
case 1 - importing Person.h in Employee.h
#import "Person.h"
@interface Employee:NSObject
{float fromEmployee;}
-(void)inEmployee;
@end
case 2 - inheriting Person.h into Employee.h
#import <Foundation/Foundation.h>
#import "Person.h"
@interface Employee:Person
{float fromInheritedEmployee;}
-(void)inInheritedEmployee;
@end
Upvotes: 1
Views: 797
Reputation: 22412
importing (or including) a file is akin to injecting the contents of that file at the location of the #import statement.
In Objective-C #import is smarter than the normal C #include in that if a file is already previously injected in an embedded #import chain, then it won't be injected again. the normal C (preprocessor) #include is not smart enough to figure that out which is why you always see constructs like
#ifndef __FOO_H
#define __FOO_H
.... header content here ...
#endif /*__FOO_H */
In C headers to ensure singular inclusions, whereas you don't see such constructs in objective C.
That is all what #import
is: a content injection mechanism.
So, taking your own example, let's say I wanted to create an instance of the Person class, I would have to #import its definition before I could do that. :
/* main.m */
#import "Person.h"
int main(int argc, char *argv[]) {
Person *person = [[Person alloc] init];
//^^^^^^^^ this won't work without the #import statement
[person release];
return 0;
}
note that nothing is being inherited, #import just injects all the pieces needed to use the Person
class in our main.m
Inheritance is, as you probably already know, a mechanism to define a new class as an extension of another class. Typically, the new class is a subclass of the parent class (think animal kingdom), but not always.
In your own example you define Employee as a sub-class of a Person.
Well in order to use a class for anything you need to #import its definition so that the compiler knows what you are talking about when you say Person
, and that includes (pun intended) creating a derived class.
Upvotes: 1
Reputation: 59307
Importing using a preprocessor directive is a job for the preprocessor, obviously, which runs before the actual compiling. That directive is interpreted as "take the contents of Person.h and place them here", so it's like if you had copied and pasted the contents of Person.h in Enployee.h.
In practical terms, this makes the compiler aware of the definitions included
in Employee.h. So, if you use an object of the Employee
class (which is
declared in Employee.h) it will know what to do with it.
This has nothing to do with inheritance, which is a completely different process which you should take a closer look in your favorite OOP book or document.
The file name is completely independent, don't let that trick you. Person.h
could be called Being.h and include class definitions for higher beings, all
in the same file, and then importing it would place it's definitions inside
the file Employee.h, which would not by any means affect from which class
Employee
inherits.
Upvotes: 3
Reputation: 137412
Importing an header file is just copying of its content to current file, e.g. declarations, macros, enums etc.
inheriting a class means that the class that you now declared is a sub-class of the inherited class, e.g. Employee is a Person.
Upvotes: 2