ha um meio satisfatorio de estender umaclasse, adicionar um atributo e ainda a relacao de identidade continuarfuncionando.A melhorsolucao para isso e: ao inves de fazer heranca, optar por composicao. Concluimos que, toda vez que uma classe e estendida eum novo atributo adicionado, isso vai quebrar o metodo equals implementado nasuperclasse. Quando uma comparacao for feita entre um objeto da sub.equals(super), o metodo dasubclasse vai querer utilizar para comparacao um atributo que so ela tem.Evitamos isso utilizandocomposicao ao inves de heranca:// Adds a value component without violating the equalscontractpublic class ColorPoint { private finalPoint point; private finalColor color; publicColorPoint(int x, int y, Color color) { if (color ==null) throw newNullPointerException(); point = new Point(x, y); this.color =color; }@Override public boolean equals(Object o) { if (!(oinstanceof ColorPoint))//Como heranca nao esta envolvida, nao e return false; //preciso permitir a comparacao de Point com //ColorPoint ColorPoint cp =(ColorPoint) o; returncp.point.equals(point) && cp.color.equals(color);}... // Remainder omitted}OBS.: Sempre comparar double com Double.compare efloat com Float.compare Item 9 Hashcode Algoritmo para gerar hashcode:1.Store some constant nonzero value, say, 17, in an int variable called result.2. Foreach significant field f in your object (each field taken into account by theequals method, that is), do the following:a.Compute an int hash code c for the field:i. If the field is a boolean, compute (f ? 1 : 0).ii. If the field is a byte, char, short, or int, compute (int) f.iii. If the field is a long, compute (int) (f ^ (f >>> 32)).iv. If the field is a float, compute Float.floatToIntBits(f).v. If the field is a double, compute Double.doubleToLongBits(f), andthen hash the resulting long as in step 2.a.iii.vi. If the field is an object reference and this classs equals methodcompares the field by recursively invoking equals, recursively invoke hashCodeon the field. If a more complex comparison is required, compute a canonicalrepresentation for this field and invoke hashCode on the canonicalrepresentation. If the value of the field is null, return 0 (or some otherconstant, but 0 is traditional).vii. If the fieldis an array, treat it as if each element were a separate field. That is,compute a hash code for each significant element by applying these rulesrecursively, and combine these values per step 2.b. If every element in anarray field is significant, you can use one of the Arrays.hashCode methodsadded in release 1.5.b. Combine the hash code c computed in step 2.a intoresult as follows: result = 31 * result+ c;3. Return result.4. When you are finished writing the hashCode method,ask yourself whether equal instances have equal hash codes. Write unit tests toverify your intuition!Ex://Lazily initialized, cached hashCodeprivatevolatile int hashCode; // (See Item 71)@Overridepublic int hashCode() { int result = hashCode; if (result == 0) { result = 17; result = 31 * result + areaCode; result = 31 * result + prefix; result = 31 * result + lineNumber; hashCode = result; } returnresult;}Item 13 Classes e Interfaces Atributosdevem ser PRIVADOS. Umaexcecao pode ser as constantes. Campos publicstatic final SEMPRE devem apontar para primitivos ou objetos imutaveis. Senao apontarem para objetos imutaveis, apesar da referencia nao poder seralterada, o objeto podera! Um