-2

I have class A, B and C with 2 methods in all 3 classes.

class A
- (void)onSuccess {
}

- (void)onFailure {
}

Then one CommonClass is there in which I’ll be doing some task. If class A is presenting CommonClass and after performing all the tasks I have to call either onSuccess() or onFailure() implemented in class A.

Which is the best way to do this, and how? In Java it's done by extend or abstract I guess.

Shakti Prakash Singh
  • 2,414
  • 5
  • 35
  • 59
user2017285
  • 283
  • 1
  • 2
  • 10
  • 3
    Your question is very unclear to me. Please elaborate to clarify this and adjust your title to reflect the actual problem. – Jeroen Vannevel Mar 07 '14 at 12:24
  • 2
    In Objective-C I believe you would define a Protocol, then you "implement" it by tacking onto your class definition by adding ``. Protocols are synonymous to interfaces. There are some vague concepts of "abstractness", but it's not inherent to the language like is is in Java. See this post: http://stackoverflow.com/questions/1034373/creating-an-abstract-class-in-objective-c – CodeChimp Mar 07 '14 at 12:28
  • 1
    @JeroenVannevel It is not unclear. In Java, you can create an abstract class, which is a hybrid Interface/Class where some methods are defined, some are not. In Objective C, there is no direct way to do this. OP is asking how would you implement something in Objective C that you would using abstract classes in Java. Pretty clear question IMHO. – CodeChimp Mar 07 '14 at 12:30
  • I'm with `JeroenVannevel` I think this is quite unclear and needs to be clarified more. – Popeye Mar 07 '14 at 14:32

2 Answers2

1

You can create a protocol class like this. This has only a .h file

@protocol RequestProtocolDelegate <NSObject>

@optional
    - (void)onSuccess;
    - (void)onFailure;
@end

and to use it in your class like this

file .h

#include "RequestProtocolDelegate.h"

@interface CommonClass : NSObject <RequestProtocolDelegate> {

}

@end

file .m

- (void)onSuccess {

}
- (void)onFailure {

}

Now you can use in your CommonClass this protocol

Fran Martin
  • 2,369
  • 22
  • 19
0

If I understood you correctly then it should be done with delegation. For exam:

//CommonClass.h

@protocol CommonDelegate <NSObject>
// list protocol methods...
- (void)onSuccess;
- (void)onFailure;
@optional

@interface CommonClass : NSObject
@property (nonatomic, strong) id delegate;
-(void)Task;
@end

//CommonClass.m
#import "CommonClass.h"
@synthesize delegate;
-(void)Task
{
    if(do smth task){
        [delegate onSuccess];
    }
    else{
        [delegate onFailure]
    }
}


// ClassA.h
#import "CommonClass.h"

@interface ClassA : NSObject <CommonClassDelegate>
@end

// ClassA.m

-(void)init
{
    CommonClass *common = [[CommonClass alloc] init];
    common.delegate = self;
    [common Task];
}

- (void)onSuccess
{
    // do smth
}
- (void)onFailure
{
    // do smth
}

... Do the same for class B and C

AntonijoDev
  • 1,317
  • 14
  • 29