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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:26:14+00:00 2026-05-11T14:26:14+00:00

Following is a part of my code for a project: public class Body extends

  • 0

Following is a part of my code for a project:

public class Body extends Point{     public double x, y, mass;      public Body() {         x = y = mass = 0;     }      public Body(double x, double y, double mass) {         this.mass = mass;         this.x = x;         this.y = y;     } }  public class Point {     public double x;     public double y;      public Point(double x, double y) {         this.x = x;         this.y = y;     } } 

I quickly realized that doing this will create two variables inside the Body class called x and two other variables in Body called y. How is this even possible, and why on earth does Java even allow it?

I assume this is the correct code of class Body:

public class Body extends Point{     public double mass;      public Body() {         super();         mass = 0;     }      public Body(double x, double y, double mass) {         super(x,y);         this.mass = mass;     } } 

Thanks for your time

  • 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. 2026-05-11T14:26:15+00:00Added an answer on May 11, 2026 at 2:26 pm

    In a sense, you are overriding fields of the super class. But it’s far easier to do accidentally because there is no overloading of fields (you only have one variable of a given name, the type doesn’t matter). This is referred to as variable ‘hiding’ or ‘shadowing’. So, you’re correct, you’ll end up with two fields with the same name.

    Your second example is correct. They are inherited from the super-class and since they are not declared private, they are visible to the subclass. It’s generally bad practice to refer directly to a super-class’s fields, and unless there is good reason, they should declared private. Your example of invoking the super constructor is the best approach.

    Also, if you hide a field with another of the same name, you can still refer to them as super.x, super.y, vs. this.x, this.y, you should avoid this situation if at all possible.

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

Sidebar

Related Questions

I have the following code from a GWT Project that is part of the
my problem is the following: This is a part of my HTML-Code: <form method='GET'
I have the following interfaces that are part of an existing project. I'd like
Following is a part of the code snippet that I will be using for
I am using the following code as part of an autocomplete script to avoid
Google optimizer includes the following snippet as part of their conversion code. Unfortunately, the
The following code is taken from here . I removed all Windows NT part
I have the following code in one cpp file which is part of a
Ok - I have the following code - on the starred part I get
I have the following code, which is run every 10ms as part of a

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.