-1

I don't seem to understand what the difference between #import (then declaring Pointer to that class) and inheritance as i can both have reference to that super class or the class included..

And, in addition to this topic, view mustn't know about view controller for loose coupling reason. does this mean that view shouldn't #include any controller and having reference to that controller??

I think I'm missing some basic concept that makes me so confused here .. Thank you for help in advance.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
denis_choe
  • 667
  • 6
  • 15
  • This is an Objective-C-specific question. Please tag appropriately. – chrylis -cautiouslyoptimistic- Mar 26 '14 at 11:58
  • [Here](http://stackoverflow.com/questions/439662/what-is-the-difference-between-import-and-include-in-objective-c) is your answer. – jnix Mar 26 '14 at 12:01
  • @jnix i didn't refer to difference between include and import maybe, i didn't make my question clear.. – denis_choe Mar 26 '14 at 12:05
  • Entirely different. All that import/include does is copy lines of code into a compilation unit. It could be a haiku for all that import/include cares. But the appropriate definitions must be copied into a compilation unit before they can be referenced for, eg, an inherited class. (You could physically copy/paste the same lines into your .m and it would be the same. Unmaintainable, of course, but the compiler wouldn't know the difference.) – Hot Licks Mar 26 '14 at 12:06
  • @chrylis i thought in general, i was wondering what is the difference between import and inheritance as both you can technically have pointer to that class...? sorry if i'm going somewhere wrong.. – denis_choe Mar 26 '14 at 12:08
  • I do not like writing that but you should really read a good book about object oriented programming. One that covers design pattern (such as mvc, delegation) wold be great. – Hermann Klecker Mar 26 '14 at 12:15

4 Answers4

1

The quick answer to your question is inheritance (:) is a universal object oriented programming principle and inclusion (#import) provides declarations of all symbols used while compiling, which is specific to C like programming languages.

In Objective-C, there is no global symbol space like there is in Java. Each file must include the definition for every symbol used.

This is done by using header files and having interfaces separated from implementations.

The Objective-C compiler only builds .m files, never .h files. The #import statement copies the contents of the .h file into the .m file at build time.

MyFoo.h

@interface MyFoo : NSObject
@end

MyBar.h

#import "MyFoo.h"

@interface MyBar : MyFoo
@end

MyBar.m

#import "MyBar.h"

@implementation MyBar
@end

At compile time only MyBar.m is compiled. To the compiler the #imports have their source dumped into MyBar.m.

How the compiler see MyBar.m

@interface MyFoo : NSObject
@end

@interface MyBar : MyFoo
@end

@implementation MyBar
@end

As you can see, the complier cares about the implementation of MyBar and uses the interfaces as a way to provide context for all the declared symbols.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
0

Importing header file is just referring code written in other file.

while inheritance very different and very powerful concept of Object oriented programming.For good understanding about this please read this.

jnix
  • 480
  • 3
  • 10
0

Short answer:-

There are no difference between these two, the only thing is #import is modified style which is being used in Objective-c. But #include is the old C style for importing the class.

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
0

maybe explaining this in a different way will help you understand..

basically, #import doesnt actually do much, its just putting a header inside your .m file.

inheritance is something completely different, this is when an object takes on the properties and functions of another object, and adds some extra functionality to it. eg, a eagle would inherit from an object, bird, since an eagle is a bird but the eagle kind of defines more than just a bird, aka has sharp talons etc.

now for you to be able to do inheritance, you will have to #import the header of the base type, eg so in your eagle class, you would need to #import "bird.h" to allow you to inherit from it, otherwise the eagle class wont know what a bird is.

the code to do the inheritance would be

//Eagle.h

#import "Bird.h"

@interface Eagle : Bird
//insert properties and functions here that would make an eagle an eagle and not just a bird
@end

hope that helps a bit with explaining the difference

Fonix
  • 11,447
  • 3
  • 45
  • 74