Ricardo Franco
Ricardo Franco

Reputation: 11

Objective C Protocols and Inheritance

Ok, there are several questions with "Protocol" and "Inheritance" but I can't really find the answer to my question. I have a class with a protocol. For example:

@class SomeClass;

@protocol SomeDelegate <NSObject>

@optional
     -(void) someMethod;

@end

@interface SomeClass : NSObject
{
     id<SomeDelegate> delegate;
}

@property id<SomeDelegate> delegate;

-(void) thisDoesStuff;

@end

Then I have a different class whose object will be a delegate of a SomeClass object:

@interface DiffClass: SomeClass<SomeDelegate>

// This method will conform to the one specified on the protocol
-(void) someMethod;

@end

My question is, does DiffClass inherit from SomeClass? I'm considering the syntax in Objective C for inheritance:

@interface ClassA : SuperClassOfClassA

Where, in the above, ClassA inherits from SuperClassOfClassA.

Additionally, in Objective C, is it possible to inherit from one class and adopt a protocol from a different class? I guess what I'm trying to get at is if it's possible that two objects are able to communicate to each other through delegates without having to inherit from that protocol's class (I hope I'm making sense).

Thanks in advance!

Upvotes: 1

Views: 2012

Answers (3)

Goppinath
Goppinath

Reputation: 10739

Answer to the question 1: with the statement

@interface DiffClass: SomeClass<SomeDelegate>

The DiffClass inherits from SomeClass and also conforms the protocol(interface) SomeDelegate.

Answer to the question 2: In Objc you can inherit only from one parent class (multiple inheritance wont be supported) but you can conform as many as protocols (interfaces) you want.

Lets take and example of an drawing App. Shape is a parent class and RectangleShape, LineSahpe, TextShape, CircleShape are the children from Shape class. All four children are inheriting from their parent Shape. But you need to move the shape except LineShape. There you can have protocol (interface) as Movable. That you can do so.

@protocol Movable <NSObject>

@end


@interface Shape : NSObject

@end


@interface RectangleShape : Shape <Movable>

@end


@interface LineSahpe : Shape // cannot be moved, just for an example.

@end


@interface TextShape : Shape <Movable>

@end


@interface CircleShape : Shape <Movable>

@end

You can have a method for the protocol like this to move all Shaped which are conforming Movable protocol (interface).

- (void)move:(id <Movable>)movableShape {

}

You can implement a communication instead of moving shapes. Protocols are really useful during advanced programming.

Hope it will help you... please feedback me.

Upvotes: 0

Mike Welsh
Mike Welsh

Reputation: 808

To answer your first question, DiffClass does inherit from SomeClass as you have it written. But it doesn't need to inherit from SomeClass. I'll be a bit more thorough below.

A protocol is a declaration of methods (and properties) that a class adopts. It does not have to be related to a class, although it often is for the delegation pattern.

For example, you could have a header that just declares a protocol. Let's call the file NewProtocol.h

@protocol NewProtocol<NSObject>
@optional
- (void)newMethod;
@end

Then any class can adopt that protocol. With your example above, this could be DiffClass. You do not need to declare the methods from NewProtocol again in the class interface.

// You would need to import NewProtocol.h
// Note that this does NOT inherit from SomeClass.
@interface DiffClass : NSObject<NewProtocol>
@end

Then the implementation of DiffClass would need to provide the declared protocol methods.

@implementation DiffClass

- (void)newMethod {
  // Do stuff.
}

@end

Then SomeClass could have a property for the declared protocol above.

@interface SomeClass : NSObject

// Often you will want weak for delegates as they can cause retain cycles otherwise.
@property(nonatomic, weak) id<NewProtocol> thingThatImplementsNewProtocol;

-(void) thisDoesStuff;

@end

DiffClass does now NOT inherit from SomeClass but can be used to communicate with SomeClass. You could declare a second protocol that SomeClass adopts and a property on DiffClass to have two way communication.

Often the protocol are declared in the same header file as a class for simplicity because they are intended as delegates for specific objects.

Upvotes: 0

gnasher729
gnasher729

Reputation: 52538

In practice, your code would be very strange.

First, you don't have a class with a protocol. You have a protocol named SomeDelegate. Then you have a class SomeClass, which is unrelated to the protocol. Well, it has an instance variable that supports SomeDelegate, but that has nothing to do with the protocol.

Then you create a class that is both a subclass of SomeClass, and supports the SomeDelegate protocol. That's unusual. I mean DiffClass both supports the protocol itself, and has a delegate supporting the protocol. That's a bit strange.

Nevertheless, DiffClass is a subclass of SomeClass, and you promised that it supports the protocol SomeDelegate, so that's fine.

But really: A protocol doesn't belong to a class. I don't know what made you think that, but you have to remove that from your brain immediately. A protocol is a totally different thing and totally independent from a class. It's a set of requirements that any class may or may not fulfil. It exists independent of any class. Because a protocol is a set of requirements, a class can support that protocol by claiming it does (adding ) and by adding the required methods.

Upvotes: 1

Related Questions