Solved

Creating an abstract class with a Protocol for child classes

Posted on 2012-04-13
2
607 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iOS10 Features 2 49
xamarin c# deserialize Json containing nested object 2 91
IIS Log files on Exchange 2013 server 6 113
Plain Text Editor for iPad 6 75
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

815 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now