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 645669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:31:43+00:00 2026-05-13T21:31:43+00:00

What are the purposes of having private/protected members of a class/structure in object-oriented programming?

  • 0

What are the purposes of having private/protected members of a class/structure in object-oriented programming? What’s the harm in having all members be public?

  • 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-13T21:31:43+00:00Added an answer on May 13, 2026 at 9:31 pm

    Encapsulation. I.e. hiding the implementation of your class data. This allows you to change it later, without breaking all client code. E.g. if you have

    class MyClass {
        public int foo;
    }
    

    your clients may write code like

    MyClass bar = new MyClass();
    bar.foo++;
    

    now if you realize that foo should actually be a double rather than int, you change it:

    class MyClass {
        public double foo;
    }
    

    and the client code fails to compile 🙁

    With a well designed interface, the change of the internals (private parts) may even include turning a member variable into a calculation or vice versa:

    class Person {
        public String getName();
        public String getStreetAddress();
        public String getZipCode();
        public String getCountryCode();
        public int hashCode();
    }
    

    (using String properties for the sake of simplicity – in a real world design some of these would probably deserve to have their own type.)

    With this design, you are free to e.g. introduce an Address property internally, which would contain street address, zip code and country code, and rewrite your accessors to use the fields of this private member instead, without your clients noticing anything.

    You could also decide freely whether to calculate the hash code every time, or to cache it into a private variable in order to improve performance. If that cache field was public, however, anyone could change it, which could ruin hash map behaviour and introduce subtle bugs. So encapsulation is key in guaranteeing the consistency of the your object’s internal state. E.g. in the above example, your setters can easily validate the zip code and country code, to prevent setting invalid values. You can even ensure that the zip code format is valid for the actual country, that is, ensure a validity criteria spanning multiple properties. With a well designed interface, you can enforce this binding by e.g. providing only a setter to set both properties at the same time:

        public void setCountryCodeAndZip(String countryCode, String zipCode);
    

    However, with public fields you simply don’t have these choices.

    A special use case for private fields is immutable objects; this is very common in e.g. Java, examples are String and BigDecimal. These classes have no public setters at all, which guarantees that their objects, once created, will not change their state. This enables a lot of performance optimizations, as well as makes them easier to use in e.g. multithreaded programs, ORM etc.

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

Sidebar

Related Questions

We need to protect a class from having static methods for security purposes. We
I'm having a problem with my code: Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal
I am having issues with networking being very slow.. For testing purposes, I tried
Having some trouble with what should be a very simple scenario. For example purposes,
I have two different classes that have the same class as a private field.
I simply want a class that does this: class cleanup : boost::noncopyable { public:
I'm learning concurrent programming and what I want to do is have a class
I'm having a little issue, for styling purposes I'm using a Definition list instead
I'm having a problem extending a class which itself further extends a abstract class.
Edit : Note that my final purpose here is not having the class working,

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.