1 Replies - 1971 Views - Last Post: 11 February 2012 - 08:47 PM Rate Topic: -----

#1 n00l3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-August 11

is not abstract and does not override abstract method compareTo

Posted 11 February 2012 - 08:35 PM

I'm studying for the CSCI A AP exam by Barron's 2009 book, and I copied this class from the book:

public abstract class Shape implements Comparable{
	// stuff
	
	public int compareTo(Object obj) {
		final double EPSILON = 1.0e-15;
		
		Shape rhs = (Shape)obj;
		double diff = area() - rhs.area();
		if (Math.abs(diff) <= EPSILON * Math.abs(area()))
			return 0;
		else if (diff < 0)
			return - 1;
		else 
			return 1;
	}
}


My question is just that if I tried changing public int compareTo(Object obj) to
public int compareTo(Shape obj)
I get: 'Circle is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable' for classes that extend Shape (like Circle or Square).
The book didn't say to do this; I just wanted to see what would happen. Because Shape is an Object, I reckoned it would be fine, and I don't understand how what I did would result in the error message or how it's got to do with being an abstract method. Doesn't the Circle and Square inherit compareTo from Shape? Could someone please explain why this/error message happens? Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: is not abstract and does not override abstract method compareTo

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: is not abstract and does not override abstract method compareTo

Posted 11 February 2012 - 08:47 PM

Class Shape implements Comparable interface and that means it have to override all methods from it. But since Shape is abstract, that is not necessary because there is no problem for an abstract class to have abstract methods. But the problem will come to other subclasses which are not abstract, every non abstract class must implements all abstract methods from its super classes which have not been implemented yet in its inheritance tree.

When you change parameters, then you are overloading, that means the compareTo() method from Comparable interface will remain abstract in Shape class and all other subclasses extending Shape, that is why you get that error message because non abstract class cant have abstract method in it.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1