Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding methods to an Objective C class interface is optional?

Coming from a C++ background, one thing that confuses me about Objective C is the fact that you can add a method to a class without actually specifying it in the class interface. So I had a barrage of questions:

  1. Why would someone choose to not add the method in the class interface?
  2. Is it simply because of visibility?
  3. Methods without a declaration in the interface are private?
  4. Is declaring methods in a class interface just optional?
  5. Is it different for overriding a base class' method?
like image 215
djcouchycouch Avatar asked Aug 20 '26 09:08

djcouchycouch


1 Answers

The main difference is that C++ sets up much of its inheritance and types at compile time and Objective C does it mostly at runtime.

The only differences in putting a method in the interface (if all parameters are objects) in objective-C are that the compiler can see it at compile time and check that an object could respond to the method - if it does not then you get a warning but the compilation does succeed and the program will run and loo for the method at runtime. If the method is in the implementation of the class or a category (or some other way) then the run time will find it and call it successfully.

There are NO private methods you can call any method.

like image 158
mmmmmm Avatar answered Aug 21 '26 21:08

mmmmmm