1

Possible Duplicate:
Creating an abstract class in Objective C

I'd like to make abstract class in Objective-C project.

But, I can't find ideas suchlike 'abstract'(in java), 'virtual'(in c++).

Doesn't Objective-C have an abstract idea? Thank you.

Community
  • 1
  • 1
akiniwa
  • 617
  • 8
  • 18

2 Answers2

12

There are no abstract classes but you can produce something similar using a combination of a class and a protocol (which is similar to Java's interface). First divide up your abstract class into those methods you wish to provide default implementations for and those you require sub-classes to implement. Now declare the default methods in an @interface and implement them in an @implementation, and declare the required methods in an @protocol. Finally derive your sub-classes from class<protocol> - a class which implements the protocol. For example:

@interface MyAbstract

- (void) methodWithDefaultImplementation;

@end

@protocol MyAbstract

- (void) methodSubclassMustImplement;

@end

@implementation MyAbstract

- (void) methodWithDefaultImplementation { ... }

@end

@interface MyConcreteClass: MyAbstract<MyAbstract>
   ...
@end

@implementation MyConcreteClass

// must implement abstract methods in protocol
- (void) methodSubclassMustImplement { ... }

@end

If you are concerned over using the same name for a class and a protocol look at Cocoa where NSObject follows this pattern...

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
  • 1
    Problem I'm finding is that if I call super on any of these methods, then I get the warning "[super class] may not respond to '[method name].'" The only way I can get rid of this warning is to implement the protocol in the abstract class too. But if I do, they effectively become public methods which defeats the object... – jowie Jul 02 '13 at 08:11
  • @jowie - I don't understand what you are trying to do. The super class does not implement the abstract methods, that is the point - all the subclasses implement the abstract methods. What do you expect a call to super for an abstract method to do? – CRD Jul 02 '13 at 08:38
  • I guess I'm probably not using the Abstract model correctly. I want my superclass to do all of the default case stuff, but then for my subclass to override certain methods, add their own stuff and be able to call super for the default stuff. – jowie Jul 02 '13 at 10:12
  • @jowie - you are describing normal inheritance. You just declare all the methods in the base class and provide default implementations for them. Any derived class can override a base class method and invoke the base class implementation using `super`. – CRD Jul 02 '13 at 10:25
  • yeah - I guess I just have to make them public because there is no protected in obj-c. – jowie Jul 02 '13 at 10:29
  • @jowie - You are correct, there are no protected *methods* in Obj-C. However there are patterns, e.g. using categories and multiple header files, which provide similar semantics at *compile time*. Go google, and if you have questions over an answer you find you can always ask it on SO. – CRD Jul 02 '13 at 18:44
  • This is really a great idea to mimic abstract class... – Easwaramoorthy Kanagaraj Nov 29 '13 at 06:32
  • @CRD I cannot call `methodSubclassMustImplement` inside `methodWithDefaultImplementation` unless `MyAbstract` class conforms to `MyAbstract` protocol. Isn't that the whole point of abstract classes? If the abstract class conforms to the protocol, then I will get a warning `method not implemented` in the abstract class. – mostruash Jan 21 '14 at 15:49
  • @mostruash you can make a private .h containing a category of your abstract class which implements the interface and then include that in your subclasses .m – malhal Sep 27 '17 at 15:41
9

Formally, no. Abstract classes are implemented by stubbing out methods in the base class and then documenting that a subclass must implement those methods. The onus is on the author to write classes that match the class contract rather than on the compiler to check for missing methods.

Objective-C has protocols, which are like Java interfaces. If you're looking for the equivalent to a pure virtual C++ class or an interface in Java, this is what you want.

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • I'd go further in saying that Objective-C really doesn't *do* abstract classes. Use protocols, which support Objective-C's duck-typing ideals, rather than C++'s common base class ones. – Wade Tregaskis Dec 10 '12 at 02:28
  • You can force overriding of key methods by having the base class implement them to throw a descriptive exception. – Catfish_Man Dec 10 '12 at 03:04
  • @WadeTregaskis: In well-formed Objective-C, I'd expect heavy use of protocols and avoidance of these sorts of *faux*-C++ features. That said, production Objective-C is (sadly) usually written by people who know plenty of C++ and not much Objective-C. Might as well put it in terms that those engineers understand. – Jonathan Grynspan Dec 10 '12 at 14:40