• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

abstract class problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 87
Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
prem pillai
Ranch Hand
Posts: 87
Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
This is my favorite tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic