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

  • Home
  • SEARCH
  • 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 8881375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:20:06+00:00 2026-06-14T20:20:06+00:00

We have two Classes (one parent and a child). both uses private variables to

  • 0

We have two Classes (one parent and a child). both uses private variables to store values, but the parent should NOT provide setters (x and y are given with the constructor and they are some sort of immutable). B should extend A with setters for x and y. Is there a common way to do so?

class A{
    private int x;
    private int y;

    A(int x, int y){
        this.x = x;
        this.y = y;
    }

}

class B extends A{

    public void setx(int x){
        this.x = x;
    }

    //same for y
}

Some thoughts

  • variables should be private
  • x and y of parent have to be immutable
  • B has to provide a public setter
  • 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-14T20:20:07+00:00Added an answer on June 14, 2026 at 8:20 pm

    If you want the variables to be immutable then it should be

    class B extends A{
    
        public B(int x, int y){
              super(x, y);
        }
    }
    

    At the moment your x and y variables in A are not immutable. To make them immutable then precede them with final

    This is the only way you can assign x and y as they are private. If you want setters then you will have to make the variables protected.

    Personally I am a big fan of immutability so would do this rather than setters – creating objects is usually quite cheap.

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

Sidebar

Related Questions

I have two classes, one is the parent of the other. They both need
I have two classes: Parent and Child , mapped one-to-many with relationship owned by
I have two classes, let's call them parent and child, and both need to
I have two classes Parent and Child . class Child extends Parent { private
Suppose I have two classes (one a parent and one a subclass). How do
I have two classes: Parent and Child with Child: belongs_to :parent and Parent has_many
Let's say I have two classes, Parent and Child. These two are related via
I basically have two child classes which have the same parent class. what I
I have two classes in an owned one-to-many relationship. The parent is Map, and
I have a bi-directional one-to-many relationship defined between Parent and Child classes. I'm looking

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.