title: Having 2 variables with the same name in a class that extends another class in Java
body: <p>Following is a part of my code for a project: </p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>I quickly realized that doing this will create <strong>two</strong> 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?</p>
  
  <p>I assume this is the correct code of class Body:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>Thanks for your time</p>