views:

38

answers:

1

hi all i am new in objective c and want to get know about inheritance ... I am aware of all concepts but confused withe the methods that programmer used to inherit a class....

Let i explain my problem.... suppose i have two classes class A and class B... and i want to make B child of A....

for that sometimes programmer used #import "class A" and sometimes used '@' sign... which one of them should be used.... and why ? is there any difference b/w their usage....

an another question is ":" sign which we write after class declaration. ex @interface class_A : class_name

...because in past i was a student of java and c# also....and there inheritance is also done in similar manner.... but in objective c (i am currently working for i phone) does it stands for same....?

thanks for your time and sorry for bad english..but please answer

+1  A: 

There is a difference between those terms, and I can see where your confusion is.

The #import is used to load definitions of a class's h file. This is, in a way, similar to C#'s using keyword, but in Objective-C we need to specify everything in the class level, not in the namespace level -- there's no concept of namespace level encapsulation in Objective-C.

The @class keyword is used whenever you need to declare that an object is valid -- but if you're going to use the internals of that object you will eventually need to add an #import of the class anyway. There's a great answer here on the difference between @class and #import.

As with C# and Java, inheritance is achieved by using the : operator in your h file. So in your declaration of Class B, it should go like:

@interface Class_B : Class_A

Hope this clears everything up.

update for your comment:

Let's say I want to inherit class A into class B, and use class C as a variable somewhere. You'll need the ff to make it work:

#import "Class_A.h"

@class Class_C;

@interface Class_B : Class_A {
    Class_C *myvariable
}

Now, lets say somewhere inside your file you need to access a Class_C member e.g., myvariable.Property1, that's the time you turn @class Class_C into #import "Class_C.h".

I don't think declaring it like this:

@class Class_A;

@interface Class_B : Class_A

would work... you'll still need an #import "Class_A.h" somewhere which makes the @class declaration somewhat redundant.

Jon Limjap
great answer (and the link also) ...it means if we use only @class1 in header file....inheritance will work 100% (i mean access all methods and variables)?. or i need to do some more...(i mean declare #include in .m file) ....voted but waiting to accept....
Ranjeet Sajwan
@ranjeet sajwan: if you want class B to inherit from class A, at the point where class B's interface is declared the compiler must already be aware of class A's interface. The main reason for this is it needs to know about class A's instance variables in order to know how big to make class B's instances. Thus to inherit, you must #import the superclass interface declaration.
JeremyP
ranjeet, I included the answer to your comment to my full answer :)
Jon Limjap
sorry for delay.... i accepted and thanks
Ranjeet Sajwan