• 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: Obj C - Inheritance
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Obj C - Inheritance


  • Subject: Re: Obj C - Inheritance
  • From: Malte Tancred <email@hidden>
  • Date: Thu, 26 Sep 2002 15:50:14 +0200

On thursday, sep 26, 2002, at 14:28 Europe/Stockholm, Alan leigh wrote:
Coming from a C++ background, it feels like I should do the following:

Abstract Base Class
- Abstract methods

Concrete Sub Class
- Implement methods

Then I can fill an array with pointers to the base class and be assured of their functionality. Is this the 'done' thing in Obj C? Or do I just do this...

Can you really be assured if their functionality?

Base Class (which I still pretend is abstract)
- General purpose methods

Sub class (which i actually make)
- Implement methods

and then rely on the fact I wont do anything silly and produce a run time error? I hope that makes sense. It seems to me to be very bad coming from C++ to do that, but also very easy :) i.e. I can just use NSMutableArray addObject and then loop through, trusting that my code has only added classes which have the thing I wanna do?

You could craft your own method that adds objects to the array e.g.:

- (void)addBase:(BaseClass *)anInstanceOfBaseClass;

The compiler will warn you if you try add something that does not qualify as a BaseClass. It will not warn you if you haven't typed the variable though.

You can also specify that the added object must adhere to a protocol, e.g.:

- (void)addBase:(id <BaseProtocol>)anObject;

Both alternatives might make the intention of the code clearer to readers but it will not by itself enforce any restrictions at runtime.

/malte

--
Malte Tancred
Oops AB
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Obj C - Inheritance (From: Alan leigh <email@hidden>)

  • Prev by Date: Re: NSUndoManager & NSTableView editing
  • Next by Date: [OT] FAQ for this mailing list
  • Previous by thread: Obj C - Inheritance
  • Next by thread: NSNetService addresses returns empty array
  • Index(es):
    • Date
    • Thread