Solved

Creating an abstract class with a Protocol for child classes

Posted on 2012-04-13
2
622 Views
Last Modified: 2016-02-10
I am new to Objective-C but I think I would like to create:

1) An Abstract class which performs certain functions needed by child classes, but should not be instantiated directly.
2) A Protocol that forces child object to conform to a standard interface
3) Child classes which inherit from this Abstract class and conform to the Protocol.

I understand that Objective-C will not let me enforce that a class is Abstract, but that's okay. I will name it with "Abstract" in the object name. I would like the child classes to be the highest level which conforms to the Protocol, otherwise I would need to make the Abstract class do so, then have empty methods to be overridden, and I am not even sure how to do this.

So please explain a different way to accomplish this or provide me the syntax to make this compile.

Thanks.

P.S. Is the "C" Topic Area the correct place to post questions for Objective-C?
0
Comment
Question by:newbieweb
2 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 37845894
Objective-C is not C++ or Java.  I'd say, it's better don't think in terms of the abstract class.

You can take a look here:
Abstract Classes with Objective-C

You'll find this code:
- (id)init
{
     [self doesNotRecognizeSelector:_cmd];
     [self release];
     return nil;
}

Open in new window


Or just use NSException and @throw.
Throwing Exceptions
@throw [NSException exceptionWithName:NSInternalInconsistencyException
                               reason:[NSString stringWithFormat:@"Override %@", NSStringFromSelector(_cmd)]
                             userInfo:nil]

Open in new window


I think the correct way is to  use the Formal Protocol.
The Basics of Protocols and Delegates

Protocols
0
 

Author Closing Comment

by:newbieweb
ID: 37848387
Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to useā€¦
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question