Simulate abstract classes and abstract methods in Objective C?

12,125

Solution 1

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).

Solution 2

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

Share:
12,125
iseeall
Author by

iseeall

Updated on July 14, 2022

Comments

  • iseeall
    iseeall almost 2 years

    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!

  • cli_hlt
    cli_hlt almost 12 years
    +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.
  • Eimantas
    Eimantas almost 12 years
    Protocols are [almost] equivalent to interfaces in Java. So that's not really what OP might want.
  • iseeall
    iseeall almost 12 years
    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.
  • SmileBot
    SmileBot about 10 years
    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.
  • Julian F. Weinert
    Julian F. Weinert over 9 years
    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 developer.apple.com/library/mac/documentation/Cocoa/Conceptu‌​al/…
  • Kris
    Kris over 7 years
    woulrdn't that make the whole if (self = [super init]) {...} return self; crash on subclasses?