Help on polymorphism in my code

Hi, I'd be glad if anyone could explain why java compiler gives errors

compiling a class of a simple application.

This is the scenario: class Point is meant to simplicistically abstract the

concept of 2D point (x and y are the coordinates). Class Point3D extends Point,

adding z coordinate. Attributes x, y and z are encapsulated (private and each

one with setter and getter methods).

There is a third class, called Ruler, its aim is to set the distance between

two 2D points or two 3D points using the polymorphic method

computeDistance(Point p1, Point p2).

The compiler fails compiling the class Ruler with this message:

Ruler.java:21: p1 is already defined in computeDistance(Point,Point)

Point3D p1 = (Point3D) p1;

^

Ruler.java:22: p2 is already defined in computeDistance(Point,Point)

Point3D p2 = (Point3D) p2;

^

2 errors

This is the relevant part of the class:

publicclass Ruler

{

privatedouble distance;

publicvoid computeDistance(Point p1, Point p2)

{

if (p1instanceof Point && p2instanceof Point)

{

// This is a 2D points case.

// Obtain x1, x2, y1, y2 via getX() and getY() then compute the

// distance.

// .....

}

elseif (p1instanceof Point3D && p2instanceof Point3D)

{

// This is a 3D points case.

// Obtain x1, x2, y1, y2, z1, z2 using getX() and getY(), getZ().

// BUT, in order to obtain z1, z2 it is needed a casting between

// objects, because Point objects don't provide getZ(), while

// Point3D do.

Point3D p1 = (Point3D) p1;

Point3D p2 = (Point3D) p2;

// ....

}

else

{

// This case is not valid.

}

}

}

If this piece of code is not enough I'll write in another post all the three

classes exactly as they are.

Thanks for any help!

[3040 byte] By [__paolo__a] at [2007-10-3 2:35:04]
# 1

> Ruler.java:21: p1 is already defined in

> computeDistance(Point,Point)

> Point3D p1 = (Point3D) p1;

> ^

> va:22: p2 is already defined in

> computeDistance(Point,Point)

> Point3D p2 = (Point3D) p2;

> ^

>

>public void computeDistance(Point p1, Point p2)

> Point3D p1 = (Point3D) p1;

>Point3D p2 = (Point3D) p2;

If I understand correctly the compiler is telling you that you have already created variables called p1 and p2. You will either have to name them something different or not try to redeclare them. Hopefully that's clear and correct. I've tried to bold the locations where I believe the error is

Message was edited by:

Aknibbs

Aknibbsa at 2007-7-14 19:34:01 > top of Java-index,Java Essentials,New To Java...
# 2

In Point, declare a computeDistance:

public double computeDistance(Point other)

{

// compute and return distance from this point to other point;

}

Override it in Point3D to account for the Z values:

public double computeDistance(Point other)

{

if (other instanceof Point3D)

{

// Cast to Point3D

// compute and return distance from this point to other point;

}

else

{

// decide how to handle (throw IllegalArgumentException, use Z=0, etc.)

}

}

Or, just try the cast, and allow the ClassCastException to fail [instead of explicitly throwing IllegalArgumentException]. Just document whatever it should do.

In Ruler, do this, and it will do the polymorphism without instanceof:

public void computeDistance(Point p1, Point p2)

{

distance = p1.computeDistance(p2);

}

MLRona at 2007-7-14 19:34:01 > top of Java-index,Java Essentials,New To Java...
# 3

Hi, thanks for your answer.

Of course I missed the point that formal parameter names may not be redeclared as local variables of the method. (http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.1)

Sorry, I'm at the beginning and there is so much to learn that often is very easy to feel lost.

__paolo__a at 2007-7-14 19:34:01 > top of Java-index,Java Essentials,New To Java...
# 4

> In Point, declare a computeDistance:

I decided to not create a computeDistance method inside of Point/Point3D because in the world of geometry, it is not true that a point is able (owns a method) to compute the distance from another point, and so it IMHO wouldn't be a good abstraction of point concept.

Criticisms will be more than appreciated!

__paolo__a at 2007-7-14 19:34:01 > top of Java-index,Java Essentials,New To Java...