• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Dog is not abstract and does not override abstract method

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm getting the following error when I try to run the code below in NetBeans:
Exception in thread "main" java.lang.ExceptionInInitializerError
at Practice16.main(Practice16.java:13)
Caused by: java.lang.RuntimeException: Uncompilable source code - Dog is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable
at Dog.<clinit>(Practice16.java:3)

Any help in explaining why I am getting this error and how to resolve it would be much appreciated.

Thank you,


 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler doesn't like being lied to. You told it that you will implement the java.lang.Comparable interface in Dog, but then you do not.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problems is that Dog declares that it implements an interface, Comparable. But it doesn't implement the methods. So the solution is to implement the methods of the Comparable interface.

Small hint: you probably want Comparable<Dog> and when netbeans says that you have compilation error don't run the code.
 
Greenhorn
Posts: 14
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bud,
You are getting this error because any class that implements the interface Comparable has to implement the method ;

Your compareTo method will return 1 of 3 values. 0 if the object passed in is equal to yours, 1 if your object is considered greater than the passed in object and -1 if it is less than. What you need to decide what is the criteria for the comparison. In your case, I would use size. Therefore your compareTo method might look like this:

 
Marshal
Posts: 80950
522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good answer, except a tiny disagreement with this bit:

Brian Enochson wrote: . . . Your compareTo method will return 1 of 3 values. 0 if the object passed in is equal to yours, 1 if your object is considered greater than the passed in object and -1 if it is less than. . . .

It returns a negative number if "less than", a positive number if "greater than" and 0 if "the same". 1, -1 and 0 are popular return values, but are not the only permissible values. It is commonly used in code like this
 
This tiny ad is wafer thin:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic