File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract class problem

 
Huan Shern Tang
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
Java Opera
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Lin Xin
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic