λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
FOR SW ENGINEERπŸ‘©‍πŸ’»/JAVA

κ΅­λΉ„JAVAμˆ˜μ—… DAY-11(2)

by μ”¨λ‹ˆ 2021. 8. 28.
728x90

κ΅­λΉ„μˆ˜μ—… 11일차 λ‘λ²ˆμ§Έ!!

이번 λ‘œκ·Έμ—μ„œλŠ” 11일차 λ‘λ²ˆμ§Έλ‘œ μ•žμ—μ„œ μ •λ¦¬ν•˜μ§€ λͺ»ν–ˆλ˜ 상속(Inheritance), μ˜€λ²„λΌμ΄λ”©(Overriding), μ˜€λ²„λ‘œλ”©(Overloading)에 λŒ€ν•΄μ„œ 정리해보겠닀!

그럼 μš”μ  μ‹œμž‘~~!!πŸ™Œ

 

1. 상속(Inheritance)

- 기쑴의 클래슀 ν™•μž₯ν•΄ μƒˆλ‘œμš΄ 클래슀 μ΄λŒμ–΄ λ‚΄λŠ” 것.

- 기쑴의 μ½”λ“œ μž¬μ‚¬μš©ν•¨μœΌλ‘œμ¨ λ‹¨μˆœμ„±, 가독성 λ†’μž„.

- λ‹¨μΌμƒμ†λ§Œ 지원.

- ν•œ ν΄λž˜μŠ€κ°€ λ‹€λ₯Έ ν΄λž˜μŠ€λ‘œλΆ€ν„° 상속 λ°›μ•„ λ§Œλ“€μ–΄μ§€λ©΄,

  μƒˆλ‘œ λ§Œλ“€μ–΄μ§„ ν΄λž˜μŠ€λŠ” : μžμ‹(child or sub)클래슀.

  λ©€λ²„λ³€μˆ˜μ™€ λ©”μ„œλ“œ λ¬Όλ €μ£ΌλŠ” 클래슀 : λΆ€λͺ¨(parent or super)클래슀.

- λΆ€λͺ¨ν΄λž˜μŠ€μ˜ λ©€λ²„λ³€μˆ˜μ™€ λ©”μ„œλ“œλŠ” μžμ‹ν΄λž˜μŠ€μ— μƒμ†λ˜μ§€λ§Œ, λΆ€λͺ¨ν΄λž˜μŠ€μ˜ μƒμ„±μžλŠ” μƒμ†λ˜μ§€ μ•ŠμŒ.

- 상속 μ‚¬μš©ν•˜λŠ” ν‚€μ›Œλ“œ : extends.

- μžλ°”μ˜ λͺ¨λ“  ν΄λž˜μŠ€λŠ” Object클래슀λ₯Ό μƒμ†λ°›μŒ --> Objectν΄λž˜μŠ€λŠ” μžλ°”μ˜ μ΅œμƒμœ„ 클래슀.

 

β–Ά μƒμ†μ˜ Bad Case

Bad Case

상속을 ν•˜μ§€ μ•ŠλŠ” λ‹€λ©΄ μœ„μ˜ 예제처럼 같은 μ½”λ“œλ₯Ό μ—¬λŸ¬λ²ˆ μž‘μ„±ν•΄μ•Όν•¨.

 

β–Ά μƒμ†μ˜ Good Case

Person클래슀λ₯Ό μƒμ†λ°›λŠ” Student, Teacher, Employee클래슀

Student, Teacher, Employeeν΄λž˜μŠ€λŠ” Person을 상속받아 nameκ³Ό ageλ₯Ό μƒˆλ‘­κ²Œ μ„ μ–Έν•˜μ§€ μ•Šμ•„λ„ μ‚¬μš©ν•  수 μžˆλ‹€.

μ΄λŠ” 상속을 μ‚¬μš©ν•˜λ©΄ λ‹¨μˆœμ„±κ³Ό 가독성을 λ†’μ—¬μ€€λ‹€λŠ” 것을 보여쀀닀.

 

 

 

2. λ©”μ„œλ“œ μž¬μ •μ˜ - μ˜€λ²„λΌμ΄λ”©(Overriding)

- λ©”μ„œλ“œ μž¬μ •μ˜ : λΆ€λͺ¨ν΄λž˜μŠ€λ‘œλΆ€ν„° 상속받은 λ©”μ„œλ“œλ₯Ό μžμ‹ν΄λž˜μŠ€μ—μ„œ ν–‰μœ„ λ°”κΎΈκ±°λ‚˜ λ³΄μ™„ν•˜κΈ° μœ„ν•΄ λ‹€μ‹œ μ •μ˜ν•΄ μ‚¬μš©

- λΆ€λͺ¨ν΄λž˜μŠ€μ—μ„œ νŠΉλ³„ν•œ μš©λ„λ‘œ μ‚¬μš©ν•˜λ˜ λ©”μ„œλ“œλ₯Ό μžμ‹ν΄λž˜μŠ€μ—μ„œ λ‹€λ₯Έ μš©λ„λ‘œ μ‚¬μš©ν•  λ•Œ ν•„μš”

- λͺ¨λ“  λΆ€λͺ¨ν΄λž˜μŠ€κ°€ μžμ‹ν΄λž˜μŠ€κ°€ μ‚¬μš©ν•˜κΈ°μ— μ ν•©ν•˜μ§€λŠ” μ•Šμ„ 수 있음. --> 일뢀 λ©”μ„œλ“œ μžμ‹ν΄λž˜μŠ€μ—μ„œ μž¬μ •μ˜ 해야함 --> μ˜€λ²„λΌμ΄λ”©(Overriding)

- λ©”μ„œλ“œκ°€ μžμ‹ν΄λž˜μŠ€μ—μ„œ μž¬μ •μ˜ λ˜μ—ˆμœΌλ©΄, μžμ‹κ°μ²΄ 톡해 λ©”μ„œλ“œ ν˜ΈμΆœν–ˆμ„ λ•Œ μƒˆλ‘­κ²Œ μž¬μ •μ˜λœ λ©”μ„œλ“œκ°€ 호좜됨.

- μž¬μ •μ˜λœ λ©”μ„œλ“œλŠ” 항상 μš°μ„ μ μœΌλ‘œ μ‹€ν–‰λœλ‹€.

 

<λ©”μ„œλ“œ μž¬μ •μ˜ κ·œμΉ™>

1. λ°˜λ“œμ‹œ 상속을 μ „μ œλ‘œν•΄μ•Όν•¨.

2. λ°˜λ“œμ‹œ λ°˜ν™˜μœ ν˜•μ΄ κ°™μ•„μ•Ό 함.

3. λ©”μ„œλ“œ 이름 κ°™μ•„μ•Ό 함.

4. 맀개 λ³€μˆ˜ 선언이 μ •ν™•νžˆ μΌμΉ˜ν•΄μ•Ό 함.

5. μ ‘κ·Όμ œν•œμž κ°™κ±°λ‚˜ 더 μ œν•œ μ—†μ–΄μ•Ό 함.

 

β–Άμ˜€λ²„λΌμ΄λ”©(Overriding)μ˜ˆμ‹œ 1번

Parentν΄λž˜μŠ€μ™€ Child클래슀λ₯Ό μ‚¬μš©ν•˜λŠ” MAIN클래슀
Parentν΄λž˜μŠ€μ™€ Parent클래슀λ₯Ό μƒμ†λ°›λŠ” Child클래슀

Parent클래슀 method2번이 Childν΄λž˜μŠ€μ—μ„œ method2번으둜 μƒˆλ‘œ μž¬μ •μ˜(Overriding) λ˜μ—ˆμŒ.

 

 

β–Άμ˜€λ²„λΌμ΄λ”©(Overriding)μ˜ˆμ‹œ 2번

- Person클래슀의 infoλ©”μ„œλ“œλ₯Ό 상속받은 Student, Teacher, Employeeν΄λž˜μŠ€λ“€μ€ 각각 infoλ©”μ„œλ“œλ₯Ό μ˜€λ²„λΌμ΄λ”©ν•΄ 각각 λ‹€λ₯Έκ°’μœΌλ‘œ μž…λ ₯받은 λ‹€λ₯Έ 값을 좜λ ₯ν•œλ‹€.

Student, Teacher, Employee클래슀 각각 μ˜€λ²„λΌμ΄λ”©ν•¨!
λΆ€λͺ¨ν΄λž˜μŠ€ Personκ³Ό Person을 μƒμ†λ°›λŠ” Student, Teacher, Emplyee클래슀

Student, Teacher, Employeeν΄λž˜μŠ€λŠ” 각각 Person클래슀 λ©€λ²„λ³€μˆ˜μΈ name, ageλ₯Ό 상속받아 μƒˆλ‘œμš΄ λ³€μˆ˜ 값을 λ°›λŠ”λ‹€. μƒˆλ‘œ 받은 λ³€μˆ˜ 값을 상속받은 info()λ©”μ„œλ“œμ— λŒ€μž…ν•΄ info()λ©”μ„œλ“œλ₯Ό μ˜€λ²„λΌμ΄λ”©ν•œλ‹€.

λ”°λΌμ„œ Mainλ¬Έλ‚΄ System.out.println으둜 각각의 λ©”μ„œλ“œλ₯Ό 받은 값은 μ„œλ‘œ λ‹€λ₯Έ μž…λ ₯받은 값이 좜λ ₯됨.

 

 

 

3. λ©”μ„œλ“œ 쀑볡 - μ˜€λ²„λ‘œλ”©(Overloading)

- μžλ°”λŠ” λ©”μ„œλ“œλ‚˜ μƒμ„±μžμ˜ 쀑볡 선언을 ν—ˆμš©ν•¨.

- 쀑볡은 λ©”μ„œλ“œ / μƒμ„±μž μ„ μ–Έν•  λ•Œ 이름은 κ°™μ§€λ§Œ λ§€κ°œλ³€μˆ˜μ˜ μœ ν˜•μ΄λ‚˜ 개수λ₯Ό λ‹€λ₯΄κ²Œ μ„ μ–Έν•΄ λ†“λŠ” 것을 의미.

- 쀑볡을 μ‚¬μš©ν•˜λ©΄ ν•˜λ‚˜μ˜ λ©”μ„œλ“œλ‘œ λ§€κ°œλ³€μˆ˜μ˜ μœ ν˜•μ— 따라 λ‹€λ₯Έ λ™μž‘μ΄ 싀행됨.

 

<μ€‘λ³΅μ˜ 쑰건>

1. 이름이 κ°™μ•„μ•Ό 함.

2. μ ‘κ·Όμ œν•œμžλ‚˜ λ°˜ν™˜μœ ν˜•μ€ 영ν–₯을 λ―ΈμΉ˜μ§€ μ•ŠμŒ.

3. λ§€κ°œλ³€μˆ˜ μœ ν˜• / 개수 / μˆœμ„œ 달라야함

 

β–Ά μ˜€λ²„λ‘œλ”© μ˜ˆμ‹œ

κ°™μ€μ΄λ¦„μ˜ λ‹€λ₯Έλ§€κ°œλ³€μˆ˜λ₯Ό 가진 6개의 μ„œλ‘œλ‹€λ₯Έ λ©”μ„œλ“œ

λ©”μ„œλ“œμ˜ 이름은 κ°™μ§€λ§Œ λ§€κ°œλ³€μˆ˜μ˜ νƒ€μž…, 개수, μˆœμ„œ λ‹€λ₯΄κΈ°λ•Œλ¬Έμ— μ΄λŠ” 6개의 μ„œλ‘œλ‹€λ₯Έ λ©”μ„œλ“œμ΄λ‹€.

728x90

'FOR SW ENGINEERπŸ‘©β€πŸ’» > JAVA' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

κ΅­λΉ„JAVAμˆ˜μ—… DAY-13  (0) 2021.09.02
κ΅­λΉ„JAVAμˆ˜μ—… DAY-12  (0) 2021.09.01
κ΅­λΉ„JAVAμˆ˜μ—… DAY-11  (0) 2021.08.28
κ΅­λΉ„JAVAμˆ˜μ—… DAY-10(2)  (0) 2021.08.28
κ΅­λΉ„JAVAμˆ˜μ—… DAY-10  (0) 2021.08.28