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 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

Ask A Question

Stats

  • Questions 232k
  • Answers 232k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I vote for #1. I know the WPF purists will… May 13, 2026 at 5:37 am
  • Editorial Team
    Editorial Team added an answer SysSound is good sample code for playing sound, but you… May 13, 2026 at 5:37 am
  • Editorial Team
    Editorial Team added an answer Change location="howdy.html" to location.href="howdy.html" And also, stop doing browser-sniffing. Do… May 13, 2026 at 5:37 am

Related Questions

I've run into a really strange bug, that I'm hoping someone can explain. I
We're writing a records management product for schools and one of the requirements is
I am trying to create a simple visualization of what my code does. I
I've come across a place in my current project where I have created several

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.