Reputation: 334
The graphic below shows a compilation error involving my if-else
conditional when attempting to implement the compareTo()
method located in my Tool
class. I am uncertain of the issue, as it would appear that the method is public
and within my Tool
class (from which the two objects being compared are constructed).
public interface Product {
public abstract String getName();
public abstract double getCost();
}
public abstract class Vehicle implements Product {
private String name;
private double cost;
public Vehicle(String name, double cost) {
this.name = name;
this.cost = cost;
}
public String getName() {
return name;
}
public double getCost() {
return cost;
}
}
public class Car extends Vehicle {
public Car(String s, double d) {
super(s, d);
}
}
public class Truck extends Vehicle {
public Truck(String s, double d) {
super(s, d);
}
}
public class Tool implements Product, Comparable<Product> {
private String name;
private double cost;
public Tool(String name, double cost) {
this.name = name;
this.cost = cost;
}
public String getName() {
return name;
}
public double getCost() {
return cost;
}
public int compareTo(Product obj) {
if (getCost() < obj.getCost()) {
return -1;
} else if (getCost() == obj.getCost()) {
return 0;
} else {
return 1;
}
}
}
import java.util.*;
public class InventoryDemo
{
public static void main(String [] args) {
ArrayList<Product> list = new ArrayList<Product>();
list.add(new Car("Jagur", 1000000));
list.add(new Car("Neon", 17000));
list.add(new Tool("JigSaw", 149.18));
list.add(new Car("Jaguar", 110000));
list.add(new Car("Neon", 17500));
list.add(new Car("Neon", 17875.32));
list.add(new Truck("RAM", 35700));
list.add(new Tool("CircularSaw", 200));
list.add(new Tool("CircularSaw", 150));
list.add(new Tool("saw1", 200));
list.add(new Tool("saw2", 150));
if(list.get(9).compareTo(list.get(10)) == 0) {
System.out.println("\nThey are the same size using compareTo().");
} else {
System.out.println("\nThey are not the same size using compareTo().");
}
}
}
Upvotes: 0
Views: 653
Reputation: 9579
The list item you are trying to call compareTo() on is a Product, because the list is declared as a list of Products:
ArrayList<Product> list = new ArrayList<Product>();
When accessing items in the list, Java is only aware that the items implement the Product interface, irrespective of whether the actual class also implements Comparable.
One solution is to define Product as extending Comparable:
public interface Product extends Comparable<Product> {
public abstract String getName();
public abstract double getCost();
}
Upvotes: 1
Reputation: 207026
Your list
is an ArrayList<Product>
, so list.get(9)
returns a Product
.
The compareTo(Product)
method is not defined in interface Product
. It's defined in class Tool
, but you're trying to call it on a Product
, which is not (always) a Tool
.
To solve this: Make your interface Product
extend Comparable<Product>
:
interface Product extends Comparable<Product> {
Ofcourse that means that any (non-abstract) class that implements interface Product
must also have a public int compareTo(Product obj)
method.
Upvotes: 1
Reputation: 6531
Your Product
interface doesn't extend Comparable<Product>
which adds
int compareTo(Product other);
list
is declared as ArrayList<Product>
, so list.get(9)
will return you Product
object.
To resolve issue you have either to make Product
extend Comparable<Product>
and implement method in Vehicle
, or, maybe, use equals() method instead, overriding default implementation. Actually the second way is preferrable, because equals()
method checks whether objects are equal, while compareTo()
tells you if this object is greater then other, or other is greater than this, or none of that is applicable - which makes equals()
usage more semantically correct in your case.
Upvotes: 1
Reputation: 2065
The Problem is your list is of type List<Product>
, but product does not implement the Comparable
interface, therefore this type does not implement the method.
Make
public interface Product extends Comparable<Product> {
public abstract String getName();
public abstract double getCost();
}
Upvotes: 1