Night Mare
Night Mare

Reputation: 13

multiple inheritance in java : implementing interfaces

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

Answers (4)

arshajii
arshajii

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

oschlueter
oschlueter

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

Raul Guiu
Raul Guiu

Reputation: 2404

You can implement more than one interface, simply separate them with commas:

class A implements Comparator<A>, Serializable {
}

Upvotes: 0

ruhungry
ruhungry

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

Related Questions