Amazon.com Widgets #import vs class inheritance
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 26, 2014, 07:41:36 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Newbie Corner
| | |-+ #import vs class inheritance
Pages: [1] Go Down
Print
Author Topic: #import vs class inheritance (Read 2275 times)
lozjoyk
Newbie
*
Posts: 8






on: August 18, 2011, 05:48:38 AM

Hi, could someone explain to me the difference between importing another classes header file and inheriting it?

and when would you use one and not the other.

Thanks,
Logged
dharr19
Full Member
***
Posts: 175


Email




Reply #1 on: August 18, 2011, 06:48:54 AM

Hi lozjoyk,

#import says to include the file into the program.  So if classA imports a file then any class that subclass classA will inherit the information from the imported file.

can someone else add to this?

cheers
david
Logged
fujilla
Global Moderator
Sr. Member
*****
Posts: 276


WWW




Reply #2 on: August 18, 2011, 07:11:59 AM

I would say that using #import is creating the "inheritance" link so:

You have ClassA with its .h/.m files

You then have ClassB which has #import "ClassA.h" in either its .h or .m files.

Now ClassB can inherit instance variables and methods from ClassA.

If you did not use #import ClassA.h into ClassB  then you could not inherit as ClassB would not know that ClassA exists.

Does that help at all?

Nick
http://myfirstiphoneapp.co.uk
Logged
lozjoyk
Newbie
*
Posts: 8






Reply #3 on: August 18, 2011, 07:40:18 AM

so when you want "ClassA" to inherit "ClassB" methods and variables you would type this...

@interface ClassB : ClassA

so why would i need an #import statement? is this if ClassA and ClassB are in seperate files i would need to import them in to become useable? and if they were in 1 file the #import statement would be not needed?


also even if i typed this (for ClassB.h)....

#import "ClassA.h"

@interface ClassB:NSObject {....

wouldnt ClassB still be able to get access to ClassA's methods and variables?



thanks for any help,

Logged
fujilla
Global Moderator
Sr. Member
*****
Posts: 276


WWW




Reply #4 on: August 18, 2011, 08:37:28 AM

I didn't fully explain the situation - sorry.

Lets start with our file called ClassA.h.  At the top of this file we have the statement:

@interface ClassA : NSObject

This says that NSObject is the superclass (also the root class) of ClassA, so ClassA inherits the methods and instance variables of NSObject.  This means you get to use alloc, init, dealloc etc..., so when you are doing say iOS Development, usually the *****AppDelegate.h file subclasses NSObject.

Now, yes, you could do:

@interface ClassB: ClassA

which would mean that ClassB inherits from ClassA, and also from NSObject as ClassA already inherits from NSObject.

So, there will be times when you use:

@interface ClassB: ClassA

and times when you would do something like:

@interface myApp: NSObject

#import myApp.h
@interface myViewController: UIViewController

etc...  it all comes down to the particular project you are working on.

Does this help any better?

Nick
Last Edit: August 18, 2011, 08:56:08 AM by fujilla Logged
lozjoyk
Newbie
*
Posts: 8






Reply #5 on: August 18, 2011, 11:05:51 AM

yes, thanks very much.

so how do you know which one to use? and are there any differences in using either to access the inherited methods / variables?

thank you,
Logged
fujilla
Global Moderator
Sr. Member
*****
Posts: 276


WWW




Reply #6 on: August 18, 2011, 11:15:37 AM

I guess the more you learn about Objective-C, and the type of development you decide to do from there, you will know which to use.

Saying that, the iOS Programming books I have been through, I have never seen subclassing of self created classes using @interface ClassA: ClassB (always used #import), so I don't really know if there are any differences between using either method.
Last Edit: August 18, 2011, 11:19:33 AM by fujilla Logged
fujilla
Global Moderator
Sr. Member
*****
Posts: 276


WWW




Reply #7 on: August 18, 2011, 11:29:14 AM

Also, one other thing to mention, if you did not already know.

As I previously mentioned you may do something like:

@interface myApp: NSObject

#import myApp.h
@interface myViewController: UIViewController

The reason myViewController a subclass of UIViewController is because of the hierarchy.  UIViewController is a grandchild of NSObject, so because in inheritance you can only look up in the hierarchy, if myViewController was a subclass of NSObject you would not be able to use the UIViewControllers methods etc... as it is below it.
Logged
lozjoyk
Newbie
*
Posts: 8






Reply #8 on: August 18, 2011, 02:00:03 PM

thanks very much, i understand this a bit better now
Logged
urbanlung
Newbie
*
Posts: 13






Reply #9 on: November 22, 2011, 02:03:41 PM

Hi, I'm kinda new to this as well and I do see where your coming from in your question. My take on the difference is that with inheritance you only have one superclass per generation. Importing gives you any number of additional objects within your object and so in a sense gives you as many  step parents as you want. However the new object will still only be the subclass of one superclass per generation. I hope that makes some sense, and with any luck fairly correct.

Cheers.
Logged
Fred
Newbie
*
Posts: 46






Reply #10 on: November 28, 2011, 01:15:37 PM

This might be too simplistic, but the #import command is a compiler command, and really has nothing to do with inheritance as far as I understand.  #import just tells the compiler to insert the code from the #import file at the location of the statement.  That fact makes all the code contained in the file available to the current file just as if it were rewritten inside the file. That is totally separate from the inheritance feature which also includes all the commands (code) of the inherited class, but inheritance is for classes.  The examples in Steve's book demonstrate the differences.  #import is used to include the other object code.  Even when you create a new class, you need to include that code in the program somewhere so there always has to be some #include statements for the new classes you create or that you will use.  #include is only used for the .h files since the .h file will reference the .m file code.

Hope this doesn't muddy the water.  When in doubt use the #include.  The compiler will not add the code a second time if it is already added by another piece of code.

Fred
Logged

Fred
Jashandeep
Newbie
*
Posts: 6


Email




Reply #11 on: December 16, 2011, 12:39:17 AM

Import : You can import a class or a package as well.In inheritance we can only inherit a class or classes .
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.