0

I have the following code -

Collection collection1 = new ArrayList();
collection1.add(new Integer(1));

Collection collection2 = new ArrayList();
collection2.add(new Integer(1));

Integer integer = new Integer(1);

System.out.println(collection1.equals(collection2));
System.out.println(collection1.equals(integer));

Output -

true
false

The result is as expected. But then shouldn't the Collection Interface method be - boolean equals(Collection c)?

But, instead it is boolean equals(Object obj)

So how can it be that a non Collection Object can be successfully passed as parameter to get boolean output true?

Payel Senapati
  • 1,134
  • 1
  • 11
  • 27
  • [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/q/2770321/12323248) – akuzminykh Feb 13 '21 at 19:35
  • No, I mean what is the necessity of the Collection Interface method being `boolean equals(Object obj)` when clearly the only way it works is `boolean equals(Collection c)` – Payel Senapati Feb 13 '21 at 19:36
  • That's why I am asking for an example where an Object which is not Collection when passed as parameter can give boolean return true – Payel Senapati Feb 13 '21 at 19:39
  • You are not getting my point at all. Please read the code I have provided kindly – Payel Senapati Feb 13 '21 at 19:42
  • See, in the Collection Interface of java there is a method - `boolean equals(Object obj)` If we have two collections - collection1 and collection2 having same elements then we can use `collection1.equals(collection2)` to get boolean output true. But I can't think of an example when we have an `Object obj` where `obj` is not collection, but still `collection.equals(object)` will return true – Payel Senapati Feb 13 '21 at 19:48
  • My question is why the java developers did not declare the method as `boolean equals(Collection c)` instead? Because every Collection is Object but not every Object is Collection – Payel Senapati Feb 13 '21 at 20:05
  • 1
    `Object o = collection1, p = collection2;` Now to `o.equals(p)`… Besides that, as said by akuzminykh, don’t use raw types. Further, don’t use `new Integer(1)`, it’s unnecessary and deprecated since Java 9. Just `add(1)` and `Integer integer = 1;` do the job. – Holger Feb 15 '21 at 10:25

1 Answers1

1

The equals method is inherited from Object and you cannot change it's signature. The implementations of the equals method will take care of verifying if the object passed is actually an instance of Collection for example. Edit: Changing the signature of equals method will result in a class with two equals method, which is unnecessary.

Dragos Ionut
  • 257
  • 1
  • 7
  • But then why not define the method as boolean equals(Collection c) when clearly an object which is not collection will never be able to return true? – Payel Senapati Feb 13 '21 at 19:52
  • 1
    Because all classes, including Collection inherits Object. This means it inherits equals method and if you try to override it with Collection instead of Object it will be overloading, so you will end up with two equals method. – Dragos Ionut Feb 13 '21 at 19:58
  • But the question is why not make the method boolean equals(Collection c) instead? Every Collection is Object but not every Object is Collection. – Payel Senapati Feb 13 '21 at 20:03
  • 1
    Why to create two methods with exactly the same functionality? Since you are forced to inherit the one from object that one should be used. The same is used also in hashmap for example, if you don't implement it your object can't be used as key. – Dragos Ionut Feb 13 '21 at 20:08
  • Since Collections are Objects as well any property of Object applies for Collection as well. So we don't require two separate methods. Only `boolean equals(Collection c)` is sufficient – Payel Senapati Feb 13 '21 at 20:13
  • 1
    You seem not to understand inheritance. The collection overrides the method from Object so it must stick to the signature given in Object. – juwil Feb 13 '21 at 20:15
  • Ok thanks, now I got my answer. Please consider putting this as answer and I will accept it. – Payel Senapati Feb 13 '21 at 20:17
  • I added the details in my answer. – Dragos Ionut Feb 13 '21 at 20:20
  • 1
    Some classes do offer both `equals` methods. But for an interface, this was not feasible. – Holger Feb 15 '21 at 10:20