Good to start:
All Java API classes, user-defined classes, or classes of any other API - extend the java.lang.Object class, implicitly or explicitly. [MALA GUPTA, 2015, p. 51]
With that in mind, when we think about implementing a class (java class design), in the context here would be the: class Conta
, we have the aspects
- Importance of the method
equals()
searching and removing values from an Arraylist.
- Importance of
override
of the method equals()
.
We should worry about the following tip:
If you are adding instances of a user-defined class as elements to an Arraylist, replace your equals() method or your methods contains()
or remove()
may not behave as expected. [MALA GUPTA, 2015, p. 281]
To avoid this it is indicated that the method is overwritten equals()
, in our case, as follows:
public boolean equals(Object obj) {
if (obj instanceof Conta) {
Conta conta = (Conta)obj;
/**
* Considerando apenas o atributo numero para se comparar os objetos.
* Alteração do método `equals()` sugerida pelo @Wryel.
*/
if (conta.numero == this.numero)
return true;
 }
return false;
}
NOTE: Modification of the method equals()
suggested by @Wryel following the concepts of DDD [Vaughn Vernon, 2013].
For a general approach we have:
Rules for method override equals()
Method equals()
defines an elaborate contract (ruleset) as follows (directly from the Java API documentation):
1. It is reflective - For any non-zero reference value x, x.equals (x) must return true. This rule states that an object must be equal to itself, which is reasonable.
2. It is symmetrical - For any non-zero reference values x and y, x.equals(y) must return true if and only if y.equals(x) returns true. This rule states that two objects must be comparable to each other in the same way.
3. It shall be transitive - For any non-zero reference values x, y, and z, if x.equals (y) returns true and y.equals (z) returns true, then x.equals (z) must return true. This rule indicates that when comparing objects, you should not selectively compare values based on the type of an object.
4. It is consistent - For any non-zero xey reference values, the multiple invocations of x.equals (y) consistently return true or consistently return false, provided that no information used in equal comparisons in objects is modified. This rule indicates that the equals() method should rely on the value of instance variables that can be accessed from memory and should not attempt to rely on values such as a system’s IP address, which can be assigned a separate value after reconnecting to a network.
5. - For any nonnull reference value x, x.equals(null) must return false. This rule indicates that a nonnull object can never be equal to null.
It’s good to consider too:
A method improperly overriding the method equals()
does not mean compilation failure.[MALA GUPTA, 2015, p. 58]
It is relevant to understand that an Arraylist is an object of the List interface, and that in our question context the method is used get()
marked below:
SOURCE: Figure 4.12 List interface methods, grouped by their functionality [MALA GUPTA, 2015, p. 277]
NOTE: There are considerations about the superscript character of the method hashCode()
, but I believe it escapes the scope of the question.
Reference:
[MALA GUPTA, 2015], OCP Java SE 7 Programmer II Certification Guide: PREPARE FOR THE 1ZO-804 EXAM
[Vaughn Vernon, 2013], Domain-Driven Design - DDD: Implementing Domain-Driven Design
Does this code compile? The class is static, and you’re giving
new Conta()
,– user28595