22

I am confused about what the difference is between a protocol and an interface? They both seem to be doing the same thing?

Is it like abstract in C# in that you are required to implement it?

nycynik
  • 7,371
  • 8
  • 62
  • 87
user333639
  • 559
  • 1
  • 4
  • 15
  • 1
    possible duplicate of [Differences between Java interfaces and Objective-C protocols?](http://stackoverflow.com/questions/990360/differences-between-java-interfaces-and-objective-c-protocols) –  Feb 11 '14 at 16:34

5 Answers5

19

In Objective C an interface is equivalent to a C++ class declaration. And a protocol is equivalent to a Java interface.

Edit: In Objective C the class definition is separated into two components called the interface and implementation, which allows you to shrink the header files. This is similar to C++. Java doesn't have an equivalent, because you implement your class functions within the class definition. C# is similar to Java in this respect.

Fletcher Moore
  • 13,558
  • 11
  • 40
  • 58
18

a protocol in Objective-C is the same as an interface in java, if thats what you mean

Len
  • 2,093
  • 4
  • 34
  • 51
  • Is it like abstract in C# in that you are required to implement it? – user333639 May 12 '10 at 11:21
  • yup, sort of. note that there are differences between an abstract classes and interfaces in c#. But an interface in C# is the same as an interface in Java – Len May 12 '10 at 11:52
4

Objective-C: protocol.

Java: interface.

Otherwise, no difference.

Paul Lynch
  • 19,769
  • 4
  • 37
  • 41
3

A protocol is a group of related properties and methods that can be implemented by any class. They are more flexible than a normal class interface, since they let you reuse a single API declaration in completely unrelated classes. This makes it possible to represent horizontal relationships on top of an existing class hierarchy.

A class interface declares the methods and properties associated with that class.

A protocol, by contrast, is used to declare methods and properties that are independent of any specific class.

Balagurubaran
  • 549
  • 6
  • 18
1
In Java - you implement an Interface
In Swift/Objective C - you conform to a Protocol

"Program to an Interface, not an Implementation"
- Design Patterns 1995
Naishta
  • 11,885
  • 4
  • 72
  • 54