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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:59:48+00:00 2026-05-28T18:59:48+00:00

Whenever I face a situation when atomic method should access private parts of different

  • 0

Whenever I face a situation when atomic method should access private parts of different classes, the IMHO-fundamental impossibility of OOP to correctly implement such thing frustrates me.

Consider a simple example: some kind of collection should track its members, and members should track their owner (for example, relationship between creatures and locations in game). I don’t see perfect way to implement addition, removal, and transition of member to/from collection that would be OOP-secure. The ‘lesser evil’ I tend to use is have doAction-methods and notify-methods. So the member-collection example would look like this (in Java):

class Member{
  private Collection owner;
  // notification methods
  public void notifyMoved(Collection to){
    owner = to;
  }
  // action methods
  public void moveTo(Collection to){
    owner.notifyRemoved(this);
    to.notifyAdded(this);
    owner = to;
  }
}

class Collection{
  private List<Member> members;
  // notification methods
  public void notifyAdded(Member item){
    members.add(item);
  }
  public void notifyRemoved(Memer item){
    members.remove(item);
  }
  // action methods
  public void add(Member item){
    item.notifyMoved(this);
    members.add(item);
  }
}

The problem is that there is possibility to break consistency by calling notification methods directly.
Is there a better way to implement such things? C++ has friends, but what about Java and OOP in general?

Edit. In Java I can place them in one package, but I don’t want to do this just to enable access to some methods. My question is more general and theoretical – where should actions (methods) be placed if they don’t relate to one specific object class; and what to do if such actions need access to their privates.

Summary of solutions

  1. Notification methods. Notifier changes internal state of object according to declared change of environment. Calling notifires manually is a threat to consistency.
  2. Language-specific accessors.
    • Java: Package-private access.
    • Java: Nested classes. (e.g. Collection being outer and Member being inner).
    • C++: Friend classes.
  • 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-28T18:59:49+00:00Added an answer on May 28, 2026 at 6:59 pm

    It’s a shortcoming of java, because it lacks the “friends” concept. You could put them in the same package and use “package” access modifier for the methods (package = no access modifier).

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

Sidebar

Related Questions

The very basic issue all developers face: Whenever user submits the form, the password
Whenever I need to check something in VS 2010 EDM designer I face the
Whenever I call this method in the Android emulator: SensorManager sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); The
I face a problem whenever the user tries to browse to second page via
Whenever I design a database, I always wonder if there is a best way
Whenever I try to add a new project to my SourceSafe repository it creates
Whenever I indent HTML in PHP mode, emacs (22.1.1, basic install on Redaht Linux
Whenever I type a (, [, or {, Notepad++ completes it with the corresponding
Whenever I'm at a break point in a certain C#.NET application in Visual Studio
Whenever I restore a backup of my database in SQL Server I am presented

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.