Reputation: 13
I have a class that implements Comparator but not that I need my class to be Serializable How can I implement both of them ?
public class A implements Comparator<A>
{
}
Upvotes: 0
Views: 71
Reputation: 129587
It's a common misconception that Java does not have multiple inheritance. It doesn't have multiple inheritance of state, but it does have multiple inheritance of (declarations of) behavior, which is shown through interfaces. So you can have a single class implement multiple interfaces:
public class A implements Comparator<A>, Serializable {
}
Upvotes: 2
Reputation: 2698
You can extend
only one superclass but you can implement
as many interfaces as you like, e.g.
public class A extends B implements Comparator<A>, Serializable {
}
Upvotes: 0
Reputation: 2404
You can implement more than one interface, simply separate them with commas:
class A implements Comparator<A>, Serializable {
}
Upvotes: 0
Reputation: 4716
import java.io.Serializable;
import java.util.Comparator;
public class A implements Comparator<A>, Serializable {
@Override
public int compare(A arg0, A arg1) {
return 0;
}
}
Upvotes: 1