[μ΄νŽ™ν‹°λΈŒ μžλ°”] Item20 μ™„λ²½κ³΅λž΅. λ””ν΄νŠΈ λ©”μ„œλ“œμ™€ Object λ©”μ„œλ“œ

2023. 3. 6. 17:08ㆍJAVA/Effective JAVA

728x90

 

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

 

Allow default methods to override Object's methods

 

mail.openjdk.org

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

 

Allow default methods to override Object's methods

 

mail.openjdk.org

4가지 μ •λ„μ˜ 이유λ₯Ό λ“€λ©° μΈν„°νŽ˜μ΄μŠ€μ˜ λ””ν΄νŠΈ λ©”μ„œλ“œλ‘œ Object λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜ν•  수 μ—†λŠ” μ΄μœ μ— λŒ€ν•΄μ„œ μ–˜κΈ°ν•˜κ³  μžˆλ‹€. 

ν•œλ²ˆμ”© μ½μ–΄λ³΄λŠ”κ±Έ μΆ”μ²œ!!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90