• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Abstract classes and methods
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Abstract classes and methods


  • Subject: Re: Abstract classes and methods
  • From: "Michael B. Johnson" <email@hidden>
  • Date: Tue, 28 Aug 2001 22:40:56 -0700

On Tuesday, August 28, 2001, at 09:11 PM, stuartbryson <email@hidden> wrote:

In C++ it is possible to force both classes and methods to be abstract.
That is classes that cannot be instantiated without being subclassed and
particular methods overrided. How do I force abstract classes and
methods in Obj-C.

what you're looking for is called a protocol in Objective-C. See Apple's Objective C book for more info.

You basically say that your class "conforms to protocol A" where that means that you promise to implement (and the compiler will check that you do :-)) all the methods in the protocol. For example, I have a file WK2DImagesHandler.h that declares a "formal protocol":

@protocol WK2DImagesHandler

- (NSArray*)images;

- (NSString*)nameForImage:(NSImage*)image;
- (NSImage*)imageForName:(NSString*)name;

...

@end

I then have a class that is a subclass of NSView and conforms to this protocol and another (one called WKHotKeysHandler):

@interface WKImageView2D : NSView <WKHotKeysHandler, WK2DImagesHandler>
{
... code
}
@end

This gives you a "weak" form of multiple inheritance, where only one class that you inherit from has any implementation. People (and Apple) talk about "informal protocols", which means that they implement the methods but don't declare the protocol as one that they conform to. I've never understood why people do this, but Apple does this a lot...

I really like protocols.


--> Michael B. Johnson, Ph.D. -- email@hidden
--> Studio Tools, Pixar Animation Studios
--> http://xenia.media.mit.edu/~wave


  • Follow-Ups:
    • Re: Abstract classes and methods
      • From: Ondra Cada <email@hidden>
  • Prev by Date: Re: Abstract classes and methods
  • Next by Date: Re: Abstract classes and methods
  • Previous by thread: Re: Abstract classes and methods
  • Next by thread: Re: Abstract classes and methods
  • Index(es):
    • Date
    • Thread