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;
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?
Actually no, you are not creating two variables with the same name, obviously a compiler should not and would not allow this.
What you are doing is shadowing the existing variables defined as x and y, meaning that Body.x and Body.y are essentially overlapping the names for Point.x and Point.y, making the latter two variable completely inaccessible from the Body class (link to Java Language Specification definition of "shadowing").
Name shadowing is generally perceiving as a bad practice and a cause of bugs, and if you turn up the javac compiler warnings, the compiler will dutifully warn you about this.