Reputation: 917
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.
Upvotes: 0
Views: 631
Reputation: 53561
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.
Upvotes: 3