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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:47:38+00:00 2026-06-10T06:47:38+00:00

I have two different classes that have the same class as a private field.

  • 0

I have two different classes that have the same class as a private field. This private field needs to be passed from one class to the other (or accessed in the other class), but I’m not sure how.

Here’s an example of what I am trying to do:

public class RealVector() {

    private double[] components;

     // Other fields and methods in here

    public double distance(RealVector vec) {
        // Some code in here
    }
}

public class Observation() {

    private RealVector attributes;

    // Other fields and methods in here
}

public class Neuron() {

    private RealVector weight;

    // Other fields and methods in here   

    public double distance(Observation obs) {
        return weight.distance(obs.attributes); // This is what I want to do, but it won't work, since attributes is private
    }   
}

For the distance method of RealVector to work it needs a RealVector passed to it, but the distance method of Neuron only has an Observation passed to it, which contains a vector as a private field. I can think of a couple of workarounds, but I don’t really like them.

1) Make Observation and Neuron extend the RealVector class. Then I wouldn’t even have to write a distance function since it would just use the superclass (RealVector) distance method. I don’t really like this solution since Observation and Neuron have a “has a” relation with the RealVector class and not an “is a” relation.

2) Have a method in the Observation class that returns the RealVector.

public RealVector getAttributes() {
    return attributes;
}  

I don’t like this solution, since it defeats the purpose of having the RealVector field private. I might as well make attributes public in this case.

I could have it return a (deep) copy of the RealVector in the class. This approach seems inefficient, since I would have to make a copy of the RealVector (essentially copy an array) every time I call getAttributes().

3) Use interfaces. Haven’t done much with interfaces, so I’m not too sure if they would be appropriate here.

Does anyone know of a way that I can keep attributes as a private member of Observation and accomplish what I’m looking to do in the distance method of Neuron?

  • 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-10T06:47:40+00:00Added an answer on June 10, 2026 at 6:47 am

    If your Observer class has a distance method which takes in a RealVector then you don’t need to expose the private RealVector attributes.

    public class Observation {
    
        private RealVector attributes;
    
        public double distance(RealVector weight){
            return weight.distance(attributes);
        }
    }
    
    public class Neuron {
    
        private RealVector weight;
    
        public double distance(Observation obs) {
            return obs.distance(weight);
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes that inherit from the same base class but do not
I have a need for two slightly different classes, that have the same members,
I have two classes that extend the same abstract class. They both need the
I have 3 different classes, a generic entity class, and then two classes that
I have two functions that do the basically same thing on two different classes....
I have two classes that represent two different database entities. Their relationship is 1:m
I'm learning about packages. I have two classes that are in different packages and
I have two different libGL libraries on the same Ubuntu 11.04 machine. One library
I have 2 classes that inherit UITableViewControllers. And this two table views should use
Is it possible to store different objects that have been extended from the same

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.