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.
3 Answers
An interface is where you define the attributes and operations of class. You must set out 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
-
11Overall, 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. 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. Nov 10, 2009 at 13:18
-
22@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. Nov 10, 2009 at 15:59
-
2From the viewpoint of Objective C only, what is the difference between the two? Both seem to enforce that certain methods be implemented by the classes that are formed based on them. Is the only difference is that protocols are classless so to speak, while interfaces need to be implemented? Oct 13, 2014 at 23:05
-
i think the dishonest part is that protocol is not an abstract class, but rather like COM dispinterface. Every smalltalk object receives message not via .x or .y as in c++ objects (analogous to c
myObject.lpVtbl->x(&myObject)
) but via a dual interface, analogous to cmyObject.lpVtbl->Invoke(&myObject, "x", &argArray);
. Due to this, protocols and interfaces seem to serve to provide the compiler with information to generate runtime and compile time typechecking. So there's no true parallel in C++ unless you introduce a script engine/COM to your project.– DmytroMar 13, 2018 at 23:10
probably good if you take a look at this + I thought it was great help to understand
From the article:
@interface
C++
Foo.h
#ifndef __FOO_H__
#define __FOO_H__
class Foo
{
...
};
Foo.cpp
#include "Foo.h"
...
Objective-C
Foo.h
@interface Foo : NSObject
{
...
}
@end
Foo.m
#import "Foo.h"
@implementation Foo
...
@end
@protocol
C++
struct MyInterface
{
void foo() = 0;
}
class A : MyInterface
{
public:
void override foo() { ... }
}
Objective-C
@protocol MyInterface
-(void) foo;
@end
@interface Foo : NSObject <MyInterface>
{
-(void) foo {...}
...
}
@end
-
2+1 Great resource. Pretty much every C/C++ programmer's go-to guide when stepping into the Objective-C realm. Apr 16, 2013 at 14:51
-
About above example
@protocol
defines some general methods,@interface
defines some custom methods and@implementation
implements that interface.@interface
has not any implementation. Also iffoo
method is same to protocol's one, so it should not be repeated on interface. Jan 16, 2016 at 14:37 -
Wow, I wish I had found that article sooner. Very helpful coming from C++ to Obj. C, highly, highly recommend anyone else in that position spend the time to check out the article linked in the answer. Jan 11, 2019 at 0:38
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.