Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't implement all method in interface Comparator?

Today, I'm trying to learn some features in Java 8, specific about Lambda Expressions. I create a new Comaparator like this :

Comparator<String> strCom = new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        return 0;
    }
};

When I read code inside Comparator interface, I have got confused. Althrough interface Comparator have two method compare() and equals(), we don't need implement all of them. I had found some reason why we don't need implement method equals() here. But i also read in javadocs

If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. What Is an Interface?

So, can someone help me understand this ? Do not override equals() is still legal ?

like image 594
Tea Avatar asked Nov 22 '16 12:11

Tea


2 Answers

equal is not needed to implement because it is inherited from the Object class, as everything in Java is an Object

As you can see in the documentation the equal Method is already defined in the Object class: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

You only need to implement the equals method if you want to check if two Comparators have the same data and therefore are "equal", but this is probably not what are you looking for, as Comparators normally do not hold any instance variables

like image 181
DZDomi Avatar answered Nov 10 '22 00:11

DZDomi


The tutorial is trying to introduce the concept of interfaces through a simple example, but it ends up being misleading.

Take this code for example:

public interface MyInterface {
    public void foo();
    public void bar();
}

public class Super {
    public void foo() { System.out.println("foo"); }
}

public class Sub extends Super implements MyInterface {
    public void bar() { System.out.println("bar"); }
}

This is perfectly valid code, despite the fact that Sub only explicitly implements one of MyInterfaces methods. It's easy to see why this is valid: foo() is already implemented by Super, and that implementation is inherited by Sub.

The exact rule goes like this:

Unless the class being declared is abstract, all the abstract member methods of each direct superinterface must be implemented (§8.4.8.1) either by a declaration in this class or by an existing method declaration inherited from the direct superclass or a direct superinterface, because a class that is not abstract is not permitted to have abstract methods (§8.1.1.1).

While the rule only talks about direct superclasses, it is technically also true for indirect superclasses, as method inheritance bubbles down through the hierarchy.

Given that equals() is implemented by Object and Object is the direct or indirect superclass of every class, you don't have to provide an implementation for equals().

like image 33
biziclop Avatar answered Nov 09 '22 22:11

biziclop