• 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: Mike Shields <email@hidden>
  • Date: Wed, 29 Aug 2001 01:02:25 -0600

On Wednesday, August 29, 2001, at 09:54 PM, stuartbryson 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.

Well, there is a protocol. Protocols are described in the ObjC book (it's in the developer docs on your machine), but here's a quickie.

Define a protocol for a set of functionality to want implemented:

@protocol MyProtocol

- (void) someMethod;
- (void) someOtherMethod:(id) sender;

@end

Then the method (or function) you want to require an object of that class is defined as:

@interface MyClass : NSObject
{
}

- (void) myMethodThatRequiresAnObjectThatImplementsCertainThings:(id <MyProtocol>) anObject;

@end

This will put up a warning at least (probably an error) which will alert you if the instance does not inherit from a class which implements the protocol.

And finally, a class which implements a protocol:

@interface MyClassThatImplementsProtocol : NSObject <MyProtocol>
{
}

- (void) someMethod; // These need to be declared in the interface
- (void) someOtherMethod:(id) sender;

@end

This is called a formal protocol because you formally determine what methods a class will implement. Informal protocols are simply a set of methods that you and the class you're going to call agree MIGHT be there, so test first before calling.

I'm finding that concepts such as abstract classes aren't needed in Obj C like in C++ since there's no reason to have to define the set of methods a class implements at compile time for the rest of the world. An example of this is the delegates all over AppKit. These are Obj C versions/examples of abstract classes. They simply implement a method and the NSApplication class asks the delegate instance if it implements the method it wants to call _at runtime_ and if it does, it calls it.

In C++ a delegate of NSApplication would need to look similar to this if written as an abstract class:

class NSApplicationDelegate
{
public:
virtual bool ApplicationShouldOpenUntitledWindow(NSApp *theApp) = 0; virtual bool ApplicationShouldTerminate(NSApp *theApp) = 0;
[ etc ]
};

then a class which wanted to be a delegate would need to implement all of these methods. You might argue that this would be an instance of a Mixin class and would have it's own default implementation of these and you'd be right, but this is just an example.

Mike


References: 
 >Abstract classes and methods (From: stuartbryson <email@hidden>)

  • Prev by Date: Re: Abstract classes and methods
  • Next by Date: Re: Subject: Is K&R Still Relevant?
  • Previous by thread: Re: Abstract classes and methods
  • Next by thread: Re: Abstract classes and methods
  • Index(es):
    • Date
    • Thread