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.