10

Simple answer would be Protocol.

The another point is that it is said that all methods in ObjectC are virtual, so no need to say virtual in ObjC.

I find it hard to understand this concept.

Any comments to figure out more clear around this question ?

Thanks for commenting.

Jan Bühler
  • 421
  • 3
  • 18
Forrest
  • 122,703
  • 20
  • 73
  • 107

2 Answers2

13

Simple answer would be Protocol.

Simple but wrong. A protocol is an interface specification. It's a collection of messages that an object must (ignoring the @optional keyword for now) respond to.

The term "virtual function" has no direct counterpart in Objective-C. In Objective-C you don't call functions on objects, you send messages to them. The object itself then decides how to respond to the message, typically by looking up the message in its class object, finding the associated method and invoking it. Note that this all happens at run time, not compile time.

The mapping between messages (or "selectors" to give them their technical term) and methods is built entirely from the @implementation. The method declarations in the @interface are only there to give the compiler the information it needs to warn you that you may have forgotten a method implementation. And it is only a warning because you can't tell until run time whether whether the object really does respond to the message or not. For example, somebody else could add a category to an existing class that provides implementations for missing methods, or a class could override forwardingTargetForSelector: to forward messages it doesn't respond to elsewhere.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • +1 Close enough. Though you can look at methods on C++ objects as sending a message to that object its just the binding of the message is done at compile time rather than runtime. – Martin York Dec 07 '10 at 09:19
  • 1
    @Martin York: Thanks for the vote. Yes, you can look on calling a function on a C++ object as sending a message, but it's not really true and it's not that helpful to do so, whereas it is true in Objective-C and it is helpful to do so. – JeremyP Dec 07 '10 at 09:29
  • 1
    I think the originators of OO would disagree with you there. That is exactly the concept of a method (A message to the object). The important difference between C++ and Obj-C is only the binding of the call to the actual method. C++ is done at compile time Obj-C is done at runtime (that is the only difference). A good summary a relavant referencees about OO-P can be found here. http://en.wikipedia.org/wiki/Object-oriented_programming – Martin York Dec 07 '10 at 16:48
  • 2
    No. Seriously. It might be the only difference but it is a fundamental difference. In Objective-C, you really do send a message. The selector is just one of the parameters to `obj_msg_send` and the object really does decide what to do with it at run time. This cannot be said for C++. I'm not saying that the Objective-C way is necessarily better, but it is fundamentally different. – JeremyP Dec 07 '10 at 17:12
2

Methods on objects in Objective-C are not virtual functions, they are real functions.

I beg to differ, methods in Obj-C aren't quite real like one would expect. They behave just like virtual functions in C++ do, except you cannot make a 'pure virtual' function in Objective-C.

Cheers, Raxit

Raxit
  • 824
  • 7
  • 12