0

I have this class which basically represents a point in X-Y plane and has this function to calculate distance between two Point Objects:

double dist(2DPoint p){
return Math.sqrt(Math.pow(x-p.x, 2) + Math.pow(y-p.y, 2));
}

I now want to create a class that represents a point in space in a class 3DPoint. I want to inherit 2d Point and create a function which calculates distance between two 3d points.

I want to do it in such a way that I could use the previous function like this:

double dist(3DPoint p)
{
    return Math.sqrt(Math.pow(z - p.z, 2) + /*Square of dist between X,Y s of the two 3d pts using the dist function from 2DPoint*/);

}

APE
  • 37
  • 4

2 Answers2

1

I'm thinking you may want to revisit a bit your knowledge of OOP, to be honest.

The thing, in my mind, is that inheritance in OOP defines what is called an "is-a" relationship between sub class and super class. For example, if you have a class Shape and a class Square or Circle, we can define an "is-a" relationship between these classes because a Circle "is-a" Shape, a Square "is-a" Shape. Any methods that operate with a Shape can operate with squares and circles because typically things that apply to a Shape will also apply to Squares and Circles.

My problem with your example is that, to me, a 3DPoint is not really the same as a 2DPoint. It will never be easy, in my mind, to convert/cast from a 3DPoint to a 2DPoint. I mean, you could try to do it by simply ignoring one of the coordinates (z for example) but it's still a bit weird to me. If I was designing this I would not make 3DPoint extends 2DPoint. I might have both classes extend something like a Vector class, but I wouldn't make 3DPoint extend 2DPoint. However, one perfectly valid method that you could create in 3DPoint would be:

class 3DPoint {
...
    public 2DPoint to2D() {
        return new 2DPoint(x, y);
    }
}

This could be useful for you.

So, finally, a piece of advice: unless the design makes sense, that is, you can clearly see an "is-a" relationship between two classes, then I'd say don't use extension/inheritance, the lines of code that you will be saving will not pay of for the wrong design in the long run, especially as your code base gets bigger.

Hope this helps in any way.

Marcio Lucca
  • 370
  • 2
  • 10
  • I only asked this question because it was in our school book's excercise for inheritance, so I thought it could possibly be done. I know it is kinda stupid. I didn't even chose the variable or function names. – APE Jun 20 '20 at 04:19
1

Try this.

class Point2D {
    double x, y;
    double dist(Point2D p) {
        return Math.hypot(x - p.x, y - p.y);
    }
}

class Point3D extends Point2D {
    double z;
    double dist(Point3D p) {
        return Math.hypot(z - p.z, super.dist(p));
    }
}
  • Will not there be a data type compatibility error because we are passing a Point3D object to a function whose formal argument is Point2D object? – APE Jun 20 '20 at 04:21
  • I used your program and it works but can you explain to me the logic behind it? – APE Jun 20 '20 at 04:30