0

As, equals() method in String class is overridden method from Object class. And during overriding, change of signature is not allowed.

But String.equals() compares string values whereas Object.equals() compares reference values. So, is signature different in both classes? Or how it's done internally?

Om Sao
  • 7,064
  • 2
  • 47
  • 61
Anuj Patel
  • 39
  • 1
  • 5
  • 1
    It is still `String#equals(Object)` so no, the signature is not changed. A quick look in the [documentation](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#equals(java.lang.Object)) would have told you this – Phil Oct 01 '19 at 07:19
  • Possible duplicate of [What is a method signature?](https://stackoverflow.com/questions/29672548/what-is-a-method-signature) – Kartik Oct 01 '19 at 07:23
  • You need a tutorial on inheritence. – Raedwald Oct 02 '19 at 10:07

1 Answers1

0

The signature of both the classes is the same but the implementation is different. For Object class:

    public boolean equals(Object obj) {
        return (this == obj);
    }

For String class the implementation is :

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

So when you call the equals method of String object it would call its overrided version of the method i.e implemented inside String class

Nishant Lakhara
  • 2,295
  • 4
  • 23
  • 46