4

Possible Duplicate:
Creating an abstract class in Objective C

In Java I like to use abstract classes to make sure that a bunch of classes has the same basic behavior, e.g.:

public abstract class A
{
// this method is seen from outside and will be called by the user
final public void doSomething()
{
// ... here do some logic which is obligatory, e.g. clean up something so that
// the inheriting classes did not have to bother with it

reallyDoIt();
}
// here the actual work is done
protected abstract void reallyDoIt();

}

Now that if class B inherits from class A, it only has to implement reallyDoIt().

How to make this in Objective C? Is it at all possible? Is it feasible in Objective C? I mean the whole paradigm seems to be different in Objective C e.g. from what I understand there is no way to forbid overriding a method (like in Java with 'final')?

Thanks!

Community
  • 1
  • 1
iseeall
  • 3,381
  • 9
  • 34
  • 43
  • See this thread [http://stackoverflow.com/questions/1034373/creating-an-abstract-class-in-objective-c](http://stackoverflow.com/questions/1034373/creating-an-abstract-class-in-objective-c) – Hamed Rajabi Varamini Jun 27 '12 at 08:45

2 Answers2

11

There is no actual constraint on not overriding a method in objective c. You can use a protocol as Dan Lister suggested in his answer but this is only good to enforce your conforming class to implement a certain behavior declared in that protocol.

A solution for an abstract class in objective c could be :

interface MyClass {

}

- (id) init;

- (id) init {
   [NSException raise:@"Invoked abstract method" format:@"Invoked abstract method"]; 
   return nil;
}

This way you can prevent methods in your abstract class to be invoked (but only at run-time unlike languages like java which can detect this when on compile-time).

giorashc
  • 13,691
  • 3
  • 35
  • 71
  • Thanks, that's actually i was thinking of doing. I just hoped there could be some way to tell in compile time to the programmer implementing the inheriting class that a method is missing and not in runtime. – iseeall Jun 27 '12 at 09:40
  • 1
    woulrdn't that make the whole `if (self = [super init]) {...} return self;` crash on subclasses? – Kris Nov 24 '16 at 14:44
4

You'll want to use something called Protocols I think.

Dan Lister
  • 2,543
  • 1
  • 21
  • 36
  • 2
    +1 And you might add that Objective-C is a language that more strongly relies on conventions than on enforcement of rules. As a developer, you will have to adhere to those conventions to make things work smoothly. – cli_hlt Jun 27 '12 at 08:37
  • 5
    Protocols are [almost] equivalent to interfaces in Java. So that's not really what OP might want. – Eimantas Jun 27 '12 at 08:49
  • I don't think abstract classes and protocols are exactly the same. Consider: Animal *duck = [Duck new]; Protocols don't work as super types like this. This kind of construct could allow dynamically taking subclasses as parameters. – SmileBot Feb 14 '14 at 13:55
  • Abstract classes are useful if you have (e.g.) a plugin architecture and you want to share **some** code and want the subclasses to override some of these methods. Read more here https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/LoadingCode/Concepts/Plugins.html#//apple_ref/doc/uid/20001272-100140 – Julian F. Weinert Dec 17 '14 at 14:04