0

My question is theoretical. In obj-c if a class implements a protocol:

@interface Class:NSObject<protocol>

And a second class inherits from the first:

@interface Class2:Class

Does Class2 implement the protocol? I want to create an interface with more than one implementation. I'm doing it by defining a father class that implements a protocol that includes all the methods that should be implemented. So I want that a programmer subclassing the father class receives a warning if the subclass doesn't implement all the methods the protocol dictates. By the way, if it matters to the response I will implement a abstract factory for the objects creation.

I read some post to do with abstract classes but I don't find the answer to my question, but other suggestions about the implementation will be well received.

Jpellat
  • 907
  • 7
  • 29
  • 1
    this will help you - http://stackoverflow.com/questions/1034373/creating-an-abstract-class-in-objective-c – rishi May 16 '12 at 09:06
  • possible duplicates: [Can ObjC protocols and categories be inherited](http://stackoverflow.com/questions/7470994/), [ObjC protocols usage](http://stackoverflow.com/questions/7617615/), [Does a subclass inherit the protocols of its parent class?](http://stackoverflow.com/questions/6058591/), [Are protocols inheritable?](http://stackoverflow.com/questions/2971843/) – jscs May 16 '12 at 18:19

1 Answers1

3

A protocol is basically a promise that your class will implement certain methods. When you subclass a class that implements a protocol, the subclass also implements the protocol, because it inherits all of the superclass's methods.

When you declare that your common superclass implements your protocol, you'll get warnings if you don't actually provide implementations for all the methods in the protocol. Your children classes inherit all these implementations, so you won't get any warnings there.

Depending on what you're actually building, using just a protocol to define the interface (methods) that must be implemented, but without any common superclass might be better. When you then create a new class that declares to implement the protocol, you'd get warnings if you haven't implemented one of the protocol's methods.

omz
  • 53,243
  • 5
  • 129
  • 141
  • That's a perfect answer! The think it's in my model I never know what implementation it's been created in the factory method, thats why I herit. For example I have a Server Class that can have a lot of implementations depending on the communication layer used. So I prefer My server to be a Server object than a id pointer because when I used in the model I don't want use a ServerImplementation object. I don't know if i'm explaining me well XD – Jpellat May 16 '12 at 09:25