Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3844796
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:08:43+00:00 2026-05-19T16:08:43+00:00

I am hoping for some clarification on how Private vs Protected vs Public works

  • 0

I am hoping for some clarification on how Private vs Protected vs Public works with respect to class members when programming in Objective-C – I thought I knew the difference (I’ve added some comments to my parent class Person with respect to the same), but the fact that the compiler did not complain when I tried to access a private ivar/member of a parent class via the subclass now has me confused.

Here is my Parent Class:

/*
 Person.h
*/

#import <Foundation/Foundation.h>

@interface Person : NSObject 
{
    //We can also define class members/iVars that are of type private
    //This means they can only be accessed by the member functions
    //of the class defining them and not subclasses
    @private
    int yob;    

    //We can also define class members/iVars that are of type public
    //Public members can be accessed directly
    @public
    bool alive;

    //By default class members/iVars are of type protected
    //This means they can only be accessed by a class's own
    //member functions and subclasses of the class and typically
    //also by friend functions of the class and the subclass
    //We can explicitly define members to be protected using the
    //@protected keyword

    @protected
    int age;
    float height;   

}
@property int age;
@property float height;
@property int yob;
@property bool alive;

@end

Here is my derived class Man:

    /*
     Man - Subclass of Person
    */

    #import <Foundation/Foundation.h>
    #import "Person.h"

    @interface Man : Person
    {
        //iVar for Man
        float mWeight;
    }
    @property float mWeight;

    @end

And finally, here is main:

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Man.h"

    int main (int argc, const char * argv[]) 
        {
            NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

            //Create a Person object
            Person * aPerson = [[Person alloc]init];

            //Create a Man object
            Man * aMan = [[Man alloc]init];

            //Let's attempt to modify our Person class members
            aPerson.height = 5.11; //Protected
            aPerson.age = 21; //Protected
            aPerson.yob = 2010; //Private
            aPerson.alive = YES; //Public

            //Let's now attempt to modify the same members via our
            //derived class Man - in theory, the private members should
            //not be accessible by the derived class man
            aMan.height = 6; //Protected
            aMan.age = 26; //Protected
            aMan.yob = 2011; //Private
            aMan.alive = YES; //Public
            aMan.mWeight = 190; //Protected member of Man Class

            [pool drain];
            return 0;
        }

Shouldn’t the compiler complain why I try to access aMan.yob above? Or by using @property & @synthesize (i.e. the setter and getter methods) have I essentially made that member protected and therefore accessible to the subclass?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T16:08:43+00:00Added an answer on May 19, 2026 at 4:08 pm

    visibility does not affect methods. methods are as good as public when visible to clients (and potential pitfalls/bugs when invisible to clients). instead, visibility affects instance variables. try this:

    #import <Foundation/Foundation.h>
    #import "Person.h"
    #import "Man.h"
    
    
        int main (int argc, const char * argv[]) 
            {
                NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
                //Create a Person object
                Person * aPerson = [[Person alloc]init];
    
                //Create a Man object
                Man * aMan = [[Man alloc]init];
    
    
                //Let's attempt to modify our Person class members
                aPerson->height = 5.11; //Protected
                aPerson->age = 21; //Protected
                aPerson->yob = 2010; //Private
                aPerson->alive = YES; //Public
    
    
                //Let's now attempt to modify the same members via our
                //derived class Man - in theory, the private members should
                //not be accessible by the derived class man
                aMan->height = 6; //Protected
                aMan->age = 26; //Protected
                aMan->yob = 2011; //Private
                aMan->alive = YES; //Public
                aMan->mWeight = 190; //Protected member of Man Class
    
    
    
                [pool drain];
                return 0;
            }
    

    this prevents the subclasses from accessing ivars directly — forcing them and clients to use the accessors (if provided).

    this is all a bit weak because categories allow clients to overcome this.

    also, older 32 bit objc programs did’t really check that the visibility was declared correctly. fortunately, that’s been deprecated in 32 and an error in 64.

    if you really want something to be private to subclasses and categories, use PIMPL with an unpublished/opaque type.

    method visibility (as found in Java, C++, etc.) is a feature i’d use in objc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm hoping I can get some clarification on how to correctly implement a Photoshop
I am hoping for some clarification on the best way to deal with handling
Hoping some learned Rails developers here can recommend an existing Ruby on Rails plugin
I was hoping some of you guru's out there know the answer to this
I was hoping for some advice on how to restrict user access to subdomains.
I am having troubles with jQuery's load function and am hoping for some help.
I was hoping to get some help with my SQL Server script. Basically, I
I'm hoping there are some fellow doctrine users out there. Here is a simplified
I'm hoping people have some ideas to help solve this problem. I am developing
I was hoping to get some advice on what to do. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.