2023. 3. 6. 17:08γJAVA/Effective JAVA
item20. μΆμν΄λμ€λ³΄λ€ μΈν°νμ΄μ€λ₯Ό μ°μ νλΌ.
" p.135 λν΄νΈ λ©μλλ equals, hashCode, toString κ°μ Object λ©μλλ₯Ό μ¬μ μν μ μκΈ° λλ¬Έμ΄λ€."
public interface MyInterface
{
default String toString()
{
return "myString";
}
default int hashCode()
{
return 10;
}
default boolean equals(Object o)
{
return true;
}
}
μ΄λ κ² μΈν°νμ΄μ€μ Object λ©μλλ₯Ό μ¬μ μνλ €κ³ νλ©΄
Default method 'toString' overrides a member of 'java.lang.Object'
λΌκ³ μ»΄νμΌ μλ¬κ° λ°μνλ€.
μ¬μ€ μΈν°νμ΄μ€λ μΈμ€ν΄μ€ νλλ₯Ό κ°μ§ μλ μκ³ , public μλ μ μ λ©€λ²λ κ°μ§ μ μκΈ° λλ¬Έμ κ΅³μ΄ μΈν°νμ΄μ€μ equalsμ κ°μ λ©μλλ₯Ό μ¬μ μνλ κ²½μ°λ κ±°μ μμ κ² κ°λ€..!
https://mail.openjdk.org/pipermail/lambda-dev/2013-March/008435.html
The key goal of adding default methods to Java was
"interface evolution", not "poor man's traits."
-> μΈν°νμ΄μ€μμ λν΄νΈ λ©μλλ₯Ό μΆκ°ν ν΅μ¬μ μΈ λͺ©μ μ "μΈν°νμ΄μ€μ μ§ν" λΌκ³ νλ€.
2. Adds complexity. Supporting this behavior had the cost of making
the inheritance model more complicated. This is definitely a negative;
there is already a lot of fear that "multiple inheritance" (as if Java
didn't already have multiple inheritance (of types) from day 1) will
make Java a lot more complicated. A great deal of effort went into
coming up with the simplest possible rules for how implementation
inheritance will work, which are:
Rule #1: Classes win over interfaces. If a class in the superclass
chain has a declaration for the method (concrete or abstract), you're
done, and defaults are irrelevant.
Rule #2: More specific interfaces win over less specific ones (where
specificity means "subtyping"). A default from List wins over a default
from Collection, regardless of where or how or how many times List and
Collection enter the inheritance graph.
κ·Έλ¦¬κ³ λ κ°μ§ κ·μΉμ΄ μ‘΄μ¬νλ€κ³ μ€λͺ νκ³ μλλ°
- ν΄λμ€κ° μΈν°νμ΄μ€λ₯Ό μ΄κΈ΄λ€.
- λ ꡬ체μ μΈ μΈν°νμ΄μ€κ° μ΄κΈ΄λ€.
λ‘ μλ κ°μ²΄λ Objectλ₯Ό μμνκ³ μλλ° μΈν°νμ΄μ€μμ λ§μ½ λν΄νΈ λ©μλλ₯Ό μ¬μ μνκ³ μλ€λ©΄.. μ΄λλ₯Ό λ°λΌμΌ νλκ°?
μ΄λ° λ¬Έμ λ μκ³ ..
3. Really only makes sense in toy examples. When designing default
methods, I talked to a number of folks like yourself who asked for this
feature. And I asked them to give me an example. Invariably, the
example was a type like List. And invariably, after some digging, it
would become clear that this feature only makes sense in situations
where the type in question was exclusively single-inherited. Giving
people a feature that is essentially multiple inheritance of behavior,
but which breaks if you actually *use* multiple inheritance, does not
seem smart.
μΈν°νμ΄μ€μ Object λ©μλλ₯Ό μ¬μ μνλ €κ³ νλ 건 μ¬μ€ μ€μ©μ μ΄μ§λ μκ³ ν μ΄ μμ μμλ κ°λ₯νκ³ μ΄μΈλ¦¬λ κΈ°λ₯μ΄λΌκ³ νλ€..
4. It's brittle. Methods like equals are really fundamental; you don't
want a classes equals() behavior changing out from under you when a
library is rev'ed and someone adds an equals() implementation to some
interface that you indirectly inherit from nine levels away. But this
is exactly what would happen if someone added an equals() method to an
existing interface, if its subtypes didn't provide their own equals().
λ§μ§λ§μΌλ‘λ μΈν°νμ΄μ€μ Object λ©μλλ₯Ό μ¬μ μνλ 건 λΆμμ νλ€.
π μ 리
https://mail.openjdk.org/pipermail/lambda-dev/2013-March/008435.html
4κ°μ§ μ λμ μ΄μ λ₯Ό λ€λ©° μΈν°νμ΄μ€μ λν΄νΈ λ©μλλ‘ Object λ©μλλ₯Ό μ¬μ μν μ μλ μ΄μ μ λν΄μ μκΈ°νκ³ μλ€.
νλ²μ© μ½μ΄λ³΄λκ±Έ μΆμ²!!