Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it not possible to create abstract classes in objective-c [closed]

Because it is not really* possible to create abstract classes in objective-c I was wondering if anyone could tell me why the creators of objective-c didn't add this feature to the language.

*of course there are a few workarounds to create a -kind of- abstract classes but thats not what i mean.

like image 770
Tieme Avatar asked Jan 11 '12 18:01

Tieme


1 Answers

If you mean why you can't declare a class abstract rather than just implement it as an abstract class, I suppose it's because it doesn't interact so well with Objective-C's extremely dynamic, types-optional, Smalltalky message-passing style (Smalltalk doesn't have built-in support for declaring them either). For example:

  • You might not know what class you're sending alloc or init to, so it would be a pretty weak guarantee

  • Subclasses might still need to pass alloc and init up the class hierarchy, so we'd need to allow that, making it an even weaker guarantee

  • Categories modify classes at runtime, making it the abstract contract weaker still

In the end, you'd wind up with a keyword that just did roughly the same thing we do now to implement abstract classes (i.e. maybe override init to bail if the receiver isn't a subclass).

Type modifiers that just specify a contract become more useful the more you rely on the static type system — but Objective-C was designed not to rely on the type system all that much, so putting the contract in the type system is less useful than in some other languages.

like image 176
Chuck Avatar answered Jan 03 '23 14:01

Chuck