Groups | Search | Server Info | Login | Register


Groups > comp.lang.objective-c > #264

A question about class extensions and private methods

From Jon Rossen <jonr17@comcast.net>
Newsgroups comp.lang.objective-c
Subject A question about class extensions and private methods
Date 2016-04-16 17:42 -0700
Organization albasani.net
Message-ID <neum6p$5eq$1@news.albasani.net> (permalink)

Show all headers | View raw


This is a fairly rudimentary question and it really has to do more with 
me understanding the book I'm using vs actually understanding the 
concept here.

Example:  Let's say you have a Fraction class that has methods to 
perform simple arithmetic operations on fractions (add, subtract, 
multiple, and divide).  You also have a reduce method that will reduce 
the fractions.  You set up your methods that perform the arithmetic to 
call the reduce method so that they return the reduced result.

You then decide to create a class extension and move the declaration of 
the reduce method into the class extension.  The expected result is that 
now only the methods in the class have access to the reduce method.  The 
reduce method now cannot be accessed directly by users of the class. 
So, in other words, by placing the reduce method in the class extension 
you cannot do something like [myFrac reduce].  The reduce method is only 
available for use by the other methods in the class.

I get this, but I'm not understanding how this is being described in my 
book.

Here's how the book starts off by describing this:

#1------
'Class extensions are useful, because their methods are private.  So, if 
you need to write a class that has data and methods that can be used 
only within the class itself, a class extension might just fit the bill.'
--------

Then the book gets more specific:
#2--------
'Suppose you only want the methods defined in the implementation section 
of the Fraction class to have access to that reduce method, that is you 
do not want to make it directly accessible by users of that class.  You 
could define that method to be private by removing it's declaration from 
the original Fraction.h header file....'

Ok, #2 pretty much describes the behavior; pretty simple concept. But is 
it just me or is #1 not very accurate or specific in what it's saying? 
When it says, 'that can be used only within the class itself', that 
seems misleading.  Am I correct in that?  For instance, a class object 
cannot use the method directly, and that object is *within* the class. 
e.g. the example I showed: [myFrac reduce] will not work.  So, this #1 
statement seems a bit vague to the point of being misleading.  Am I 
misunderstanding what 'within the class itself' means or is the author 
not being precise enough here to actually describe the situation accurately?

Thanks for any info.
jonR

Back to comp.lang.objective-c | Previous | NextNext in thread | Find similar


Thread

A question about class extensions and private methods Jon Rossen <jonr17@comcast.net> - 2016-04-16 17:42 -0700
  Re: A question about class extensions and private methods spikeysnack <spikeysnack@gmail.com> - 2016-04-19 01:05 -0700
    Re: A question about class extensions and private methods Jon Rossen <jonr17@comcast.net> - 2016-04-19 16:01 -0700

csiph-web