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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:23:55+00:00 2026-06-07T03:23:55+00:00

I instantiate two objects of one class, and one object accesses and changes private

  • 0

I instantiate two objects of one class, and one object accesses and changes private fields of the other object. I believe this violates the private access control. It breaks encapsulation in OOP. However, I find that both C++ and Java seem to allow this “violation”. Please enlighten me why this “violation” is allowed!

Sample C++ code:

#include <iostream>
using namespace std;

class Person
{
    private:
        int age;
    public:
        void changeAge(Person & p, int k) // change peer object's private fields
        {
            p.age = k;
        };

        Person(int k)
        {
            age = k;
        };

        void showAge()
        {
            cout << this->age << endl;
        };
};

int main()
{
    Person a(10);
    a.showAge();
    Person b(11);
    b.changeAge(a, 20); // I think the compiler SHOULD generate access violation error
    a.showAge();
    b.showAge();
    return 0;
};

The output is:

10
20
11

You may write similar code for Java, and you’ll have the same output.

I consulted http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
I understand private fields are visible at the class level. I believe that’s probably the only viable reason for this “violation” to take place. (Please add more reasons if you can!)

However, I don’t think it makes sense to allow this “violation”, because it breaks encapsulation in OOP. The fact that we both are human beings does NOT imply that I can access and modify your private stuff. It’s no longer your “private stuff” if I can.

Please enlighten me! Is the “violation” a bug of C++ & Java? If I’m wrong, please let me know!

  • 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-06-07T03:23:57+00:00Added an answer on June 7, 2026 at 3:23 am

    It is definitely not a “bug” in either language. They are well established, and were designed to operate this way. Access controls operate at the class level, not the object level. So when Person makes something private, that means that only code in the Person class can manipulate it, but the object in question is irrelevant.

    The point was to isolate knowledge of the internals of objects to particular pieces of code, not to ensure that one object couldn’t manipulate another one.

    Don’t make the mistake of thinking that OOP is some well-established set of rules. Many different languages implement different ideas from OO, and there are differences among all of them. If a resource tells you definitively that OO “means” any specific thing, then they are misleading you. There’s plenty that’s open to interpretation by language designers and program authors.

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

Sidebar

Related Questions

My Control Class basically picks which object / class to instantiate. Because this is
I have a class that instantiates two classes which implement interfaces. I want one
Let's say I instantiate an object in Javascript like this: var myObj = new
How can I instantiate a class by throwing in a variable name? Consider this
Can I avoid instantiate a Db object inside of Names object to access it
I have two objects - one that contains some code with will fire an
I'm writing a Java application that will instantiate objects of a class to represent
If i create a singleton class then only one object of that class can
I have a Java class called CreateThread which creates one thread, everytime an object
I have two executables that access a DLL containing Data Access Layer objects Each

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.