TheAmateurProgrammer
TheAmateurProgrammer

Reputation: 9392

What is the difference between a method and a protocol

They say that a protocol is a method, but it's different from a method. So exactly, what does it do and what is the difference between a method and a protocol?

Upvotes: 1

Views: 3092

Answers (3)

Akusete
Akusete

Reputation: 10794

In Objective-C each class has an interface, a list of method defenitions which that class implements, and an implementation, the source for each of those method defenitions.

A protocol is a list of method definitions. A class can support a protocol by implementing the methods it defines.

Upvotes: 1

Firoze Lafeer
Firoze Lafeer

Reputation: 17143

A protocol is a set of methods that objects of a class may implement.

Let's say you have a protocol that consists of a method A and a method B, then an object conforms to that protocol if it implements method A and method B.

It's also possible for a protocol to include optional methods which are not required to be implemented.

Upvotes: 0

ennuikiller
ennuikiller

Reputation: 46965

A protocol is most definitely NOT a method! A protocol is a set of methods that a class implements when conforming to that protocol. It is similar to a Java interface. See this informative discussion: Objective-C versus Java Interface

Upvotes: 5

Related Questions