aspose file tools*
The moose likes Beginning Java and the fly likes Dog is not abstract and does not override abstract method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dog is not abstract and does not override abstract method " Watch "Dog is not abstract and does not override abstract method " New topic
Author

Dog is not abstract and does not override abstract method

Bud Tippins
Ranch Hand

Joined: Jan 28, 2011
Posts: 52
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,


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Brian Enochson
Greenhorn

Joined: Apr 12, 2011
Posts: 14

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:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
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
 
wood burning stoves
 
subject: Dog is not abstract and does not override abstract method