2

i have the following classes:

Teacher
Student
Class (like a school class)

They all extend from KObject that has the following code:

- initWithKey
- send
- processKey

Teacher, Student Class all use the function processKey and initWithKey from KObject parent class. They implement their own version of send. The problem I have is that KObject should not be instantiated ever. It is more like an abstract class, but there is no abstract class concept in objective-c. It is only useful for allowing subclasses to have access to one property and two functions.

What can I do so that KObject cannot be instantiated but still allow subclasses to have access to the functions and properties of KObject?

3
  • this kind of questions are probably more suitable for programmers.stackexchange.com Commented Oct 20, 2013 at 0:39
  • I'm not sure I understand your problem. You're writing the program, right? So, just don't ever instantiate a KObject. Why do you think you need to make it impossible to instantiate it? Commented Oct 20, 2013 at 1:12
  • 1
    By the way, do not name your class Class! It's a reserved Objective-C type. Name it SchoolClass or something. Commented Oct 20, 2013 at 7:50

3 Answers 3

5

Abstract classes are very common in Objective-C, and the class cluster - a widely used pattern in Cocoa - is a variation on the abstract factory pattern.

As you've noted however, there's no language facility to explicitly demarcate a method or class as abstract - this is typically done in the documentation. If you require additional safety to ensure the class is not being used in unintended ways you can do the following:

Initializer:

//Invocation of the initializer in a sub-class will not raise the exception. 
if ([self class] == [MyAbstractClass class]) 
{
   [NSException raise: . . . class is abstract - use subclass. 
}

Method:

- (BOOL)someAbstractMethod
{
    [NSException raise:NSInvalidArgumentException format:@"%@ is abstract", 
        NSStringFromSelector(_cmd)];
    return NO;
}

Protocol vs Abstract Base

I disagree with statement "it is better to use a protocol" made in some other answers. While it is possible to combine an abstract base class with a protocol, it is not necessarily better.

When to Use a Protocol

Use a protocol to specify an integration contract - like a plugin architecture. An example would be a "media player", where the implementation of 'play' for a movie and an audio stream would be totally different.

When to Use an Abstract Base Class (or class cluster)

Use an abstract base class when some of the behavior between a class hierarchy is shared, and some of the implementation details vary between specific sub-types. . Its not necessarily better to use a protocol here unless you wish to communicate the intention that this set of methods is swappable for another implementation.

Class Cluster:

With a class cluster the factory methods to to obtain an instance of one of the sub-types are on the base-class itself. There are times when this makes for nicely readable and cohesive code. (Probably not relevant to your specific example, but an interesting point relating to abstract classes in Objective-C)

Sign up to request clarification or add additional context in comments.

Comments

0

A hacky solution:

- (id)init {   if ([self class] == [FastEnumerable class]) {
    @throw [NSException exceptionWithName:NSInternalInconsistencyException
      reason:@"Error, attempting to instantiate KObject abstract class directly." userInfo:nil];   }

  self = [super init];   if (self) {
    // Initialization code here.   }

  return self; }

And it is better to use a protocol to make "send" as an abstract method...

Comments

-1

As you wrote abstract classes is not supported in Objective-C so most likely you should not try to "fix" that. Objective-C is not built like other languages like c++/Java/C-Sharp language and don't support some features those languages do. So instead of trying to code "Java with Objective-C" embrace the differences and try to do stuff the "Objective-C" way when you are coding "Objective-C" instead of doing hacks thats likely to only make it harder instead of easier for other programmers trying to use or do maintenance on your code.

3 Comments

Please take a look at my answer, while there is no specific language facility to mark a class or method as abstract, the pattern is very common in obj-c. No doubt you've heard of the class cluster? Ever wondered how they work?
I did look at your answer and it's a good one. I did know about class clusters just not the exact implementation details. Class clusters are not the way i would think about abstract classes in general since you use them to instantiate your actual type. And if you try to do NSNumber *number = [[NSNumber alloc] init]; it does not raise any exception so in that particular instance apple do not enforce the abstractness of the class. But i agree that as a concept abstract classes and methods exist in objective-c.
What inspired me to this answer was that i se a lot of questions here where people are trying to get around the fact that objective-c misses some stuff to enforce certain object oriented principles such as protected variables/methods sealed/final classes and abstract classes etc. There is no support to enforce this and apple framework code don't seem to try to do so. Instead it is common practice to state your intent when documenting your class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.