| Author |
abstract class problem
|
Huan Shern Tang
Greenhorn
Joined: Mar 05, 2011
Posts: 1
|
|
Hi guys, I'm new here.
I'm trying to make an abstract class Point with concrete classes of Point1D, Point2D, Point3D, with a general method in Point to compute the distance between current point to origin despite its type of Point1D, 2D or 3D. I came up with this:
public abstract class Point {
private double distance;
protected Point() {
distance = 0.0;
}
public abstract void computeDistance();
}
public class Point1D extends Point {
void computeDistance(double dist) {
System.out.println("Distance is " +dist);
}
}
When I tried to compile Point1D, I get this error message: Point1D is not abstract and does not override abstract method computeDistance() in Point
When I tried to make Point1D an abstract class, it compiles just fine. But that would defeat the whole purpose of Point being an abstract class for the concrete classes Point1D-3D.
So I guess my question is, how do I make Point1D a concrete class without the compiler complaining?
Thanks for your time! Any help is appreciated
|
 |
prem pillai
Ranch Hand
Joined: Nov 02, 2007
Posts: 87
|
|
Point1D is not abstract and does not override abstract method computeDistance() in Point
Notice what compiler says ... its saying you haven't overidden the abstract method computeDistance() ... Have a close look at the signature of the abstract method in your base class and the computeDistance(..) method you have implemented in your child class. If you are not able to find the difference , then you might want to have a look at how overriding works.
|
 |
prem pillai
Ranch Hand
Joined: Nov 02, 2007
Posts: 87
|
|
And welcome to JavaRanch
|
 |
Lin Xin
Greenhorn
Joined: Mar 05, 2011
Posts: 1
|
|
|
|
 |
prem pillai
Ranch Hand
Joined: Nov 02, 2007
Posts: 87
|
|
Lin Xin , two things;
1)Try to avoid giving direct answers. Direct answers won't really help to learn.
2)Huan Shern Tang , was asking how to override and not
|
 |
prem pillai
Ranch Hand
Joined: Nov 02, 2007
Posts: 87
|
|
And welcome to JavaRanch
|
 |
 |
|
|
subject: abstract class problem
|
|
|