I would like to know what the @interface in objective C is? is it just where the programmer want to declare the variables, class name or method names...? I am not sure whether it is like interface in Java. And about the @protocol in objective C as well. It seems like the interface in Java more. Could anyone give me detail explanation please. I truly appreciate it.
+8
A:
An interface is where you define the attributes and operations of class. You must setout the implementation too.
A protocol is like an interface for java.
e.g.
@protocol Printing
-(void) print;
@end
can be implemented
by declaring (confusingly in the interface)
@interface Fraction: NSObject <Printing, NSCopying> {
//etc..
The confusing thing for java developers is that the curly braces {}
are not the end of the interface e.g.
@interface Forwarder : Object
{
id recipient;
} //This is not the end of the interface - just the operations
- (id) recipient;
- (id) setRecipient:(id) _recipient;
//these are attributes.
@end
//This is the end of the interface
John Nolan
2009-11-05 08:42:03
Overall, pretty good explanation, but comes off as somewhat biased that Java does it the "better" way. As with so many things, the terminology is relative to the language. It helps to remember that Objective-C predates Java, and Java drew quite a bit from it, including the concept of interfaces from protocols. http://www.virtualschool.edu/objectivec/influenceOnJava.html It would arguably have been less confusing if Java had retained the same name, but they didn't because Java doesn't have a separate .h file which (in C/C++) contains the "interface" for a compilation unit.
Quinn Taylor
2009-11-10 13:18:45
@QuinnTaylor I didn't want to say 'Java does it better' but I phrased it that way as the OP asked the question with relation to the Java language.
John Nolan
2009-11-10 15:59:10
+1
A:
The @interface
in Objective-C has nothing to do with Java interfaces. It simply declares a public interface of a class, its public API. (And member variables, as you have already observed.) Java-style interfaces are called protocols in Objective-C and are declared using the @protocol
directive. You should read The Objective-C Programming Language by Apple, it’s a good book – short and very accessible.
zoul
2009-11-05 08:47:31