Live Study - Java μμ (Inheritance)
μ 리ν λ΄μ©
- Java μ μμκ³Ό νΉμ§
- Super
- μ€λ²λΌμ΄λ©
- μ μ΄μ
- λ€νμ±
- Dynamic Method Dispatch
- μΆμ ν΄λμ€
- Object ν΄λμ€
Java μ μμ(Inheritance)κ³Ό νΉμ§
ν΄λμ€λ₯Ό μμ λ°λ λ€λ κ²μ κΈ°μ‘΄μ ν΄λμ€λ₯Ό μ¬μ¬μ©ν΄μ μλ‘μ΄ ν΄λμ€λ₯Ό μμ±νλ κ²μ΄λ€.
μμμ λͺ©μ λ κ²°κ΅ μ€λ³΅μ μ κ±°μΈ κ² κ°λ€. νλ‘κ·Έλλ°μ λ§μ κ°λ
μ μ½λμ μ¬μ¬μ©μ±μ λμ΄κ³ , μ€λ³΅μ μ κ±°νκΈ° μν΄ λ§λ€μ΄μ§κ³ λ°μ λλ κ²λ€μ΄ μ λ§ λ§μκ² κ°κ³ , μμμ΄λΌλ κ°λ
λ κ·Έ κ³Όμ μμ μ°λ¬Όμ΄λΌκ³ μκ°νλ€.
Java μμ ν΄λμ€λ₯Ό μμ λ°κΈ° μν΄μλ extends λΌλ ν€μλλ₯Ό μ¬μ©νλ€.
class Product {
String productCode;
String productType;
}
public class Book extends Product {
String author;
String title;
// ..
}
μμ λ°λ Book κ°μ ν΄λμ€λ₯Ό Child(μμ), Sub(νμ), Derived(νμ) ν΄λμ€ λΌκ³ λ§μ΄ νννκ³ , μμμ ν΄μ£Όλ Product κ°μ ν΄λμ€λ₯Ό Parent(λΆλͺ¨), Super(μμ), Base(κΈ°λ°) ν΄λμ€λΌκ³ μμ£Ό νννλ€.
μμ ν΄λμ€λ λΆλͺ¨ ν΄λμ€μ λ©€λ² λ³μ, λ©μλλ€μ λͺ¨λ μμ λ°κ² λλ€. μ΄ λ μ κ·Ό μ μ΄μλ‘ μμ λ°μ§ λͺ»νλ μμλ€λ μμ§λ§, λΆλͺ¨ ν΄λμ€μ μμμ μμ ν΄λμ€μμ μ¬μ©ν μ μλ€. κ·Έλ¦¬κ³ μμ λ°μ§ λͺ»νλ κ²λ€λ μλλ°, μμ±μμ μ΄κΈ°ν λΈλ‘μ μμλμ§ μλλ€.
μμ ν΄λμ€λ λΆλͺ¨ ν΄λμ€μ λͺ¨λ λ©€λ²λ₯Ό μμ λ°κΈ° λλ¬Έμ μ΄λ₯Ό νμ₯, Extend λΌκ³ ννν μ μκ³ , κ·Έλμ μμ λ°μ λμ ν€μλκ° extends μΈ μ΄μ λΌκ³ νλ€.
class Product {
String productCode;
String productType;
}
class Book extends Product {
String author;
String title;
void display() {
System.out.printf("%s, %s, %s, %s%n",
this.productCode, this.productType, this.author, this.title);
}
}
μμμ΄λΌλ건 μ΄μ¨λ ν΄λμ€ κ°μ κ΄κ³λ₯Ό λ§λ€μ΄μ μ€λ³΅μ μ€μ΄κ³ μ¬μ¬μ©μ±μ λμΈ λ°©λ²μ΄λ€.
μμμ΄ μλλΌ ν΄λμ€ κ°μ ν¬ν¨μ΄λΌλ κ΄κ³λ₯Ό λ§Ίμ΄ μ’μ ꡬ쑰μ μ½λλ₯Ό μμ±ν μλ μλ€.
class Car {
Wheel[] wheels = new Wheel[4];
// ..
}
class Wheel {
String brand;
String type;
}
μμ ν΄λμ€λ 'μλμ°¨κ° λ°ν΄λ₯Ό κ°μ§κ³ μλ€' λΌκ³ ννν μ μλ€. μλμ°¨λΌλ 컀λν ν΄λμ€λ₯Ό μμ±νλ κ²λ³΄λ€ κ΅¬μ± μμ λ³λ‘ μ¬λ¬κ°μ ν΄λμ€λ₯Ό λ§λ€κ³ ν¬ν¨ κ΄κ³λ‘ μ¬μ¬μ©νλ©΄, λ³΄λ€ μ’μ ꡬ쑰μ νλ‘κ·Έλ¨μ λ§λ€ μ μλ κ² κ°λ€.
ν΄λμ€μ κ΄κ³λ₯Ό κ²°μ ν λ 'Aλ Bμ΄λ€(is - a)', 'Aλ Bλ₯Ό κ°μ§κ³ μλ€(has - a)' μ λμ
ν΄μ μκ°ν΄λ³΄λ©΄ κ°λ₯μ μ‘μ μ μλ κ² κ°λ€.
μ±
μ μνμ΄λ€. (is - a)
μ°¨λ λ°ν΄λ€μ κ°μ§κ³ μλ€. (has - a)
λ€μ μμμΌλ‘ λμμμ, Java λ νλμ ν΄λμ€λ§ μμ λ°μ μ μλ€. λ¨μΌ μμ μ΄λΌκ³ νννλ€. λ€μ€ μμμ νμ©νλ μΈμ΄(C++, Python λ±)λ μλ€.
λ€μ€ μμμ νμ©νλ κ²½μ° μ¬λ¬ ν΄λμ€μμ μμμ λ°μ μ μκ² λλ―λ‘ λ³΅ν©μ μΈ κΈ°λ₯μ κ°μ§ ν΄λμ€λ₯Ό μ½κ² μμ±ν μ μκ² λμ§λ§, μμ λ°λ ν΄λμ€λ€ μ€ κ°μ μ΄λ¦μ λ©μλκ° μλ κ²½μ° κ΅¬λ³νκΈ° μ΄λ ΅λ€λ λ¨μ μ΄ μλ€.
κ·Έλμ νμ΄μ¬μμλ Method Resolution Order (MRO) λΌλ κ°λ μ΄ μμ΄μ μμ μν©μμ μ΄λ€ λ©μλκ° μ¬μ©λλμ§μ λν κ·μΉμ΄ μλ€.
Java μμλ μμ κ°μ μ΄μ λ‘ λ€μ€ μμμ νμ©νμ§ μλλ€. ν΄λμ€ κ°μ κ΄κ³κ° λ³΄λ€ λͺ
νν΄μ§λ μ₯μ μ΄ μλ€.
Super
super λ μμ ν΄λμ€μμ λΆλͺ¨ ν΄λμ€μ μμμ μ°Έμ‘°νλλ° μ¬μ©λλ λ³μλ€.
λΆλͺ¨ ν΄λμ€μ μμλ this ν€μλλ₯Ό μ¬μ©ν΄ μ°Έμ‘°ν μ μκ³ , μμ ν΄λμ€μ μμκ³Ό λΆλͺ¨ ν΄λμ€μ μμμ ꡬλ³ν΄μ μ¬μ©ν΄μΌ ν λ super ν€μλλ₯Ό μ¬μ©νλ©΄ λλ€.
class Product {
String productCode;
String type = "Product Type";
}
class Book extends Product {
String author;
String type = "Book Type";
void displayType() {
System.out.println(String.format("Super: %s, This: %s", super.type, this.type));
}
}
public class SampleTest {
public static void main(String[] args) {
Book book = new Book();
book.displayType();
}
}
// μΆλ ₯κ²°κ³Ό
// Super: Product Type, This: Book Type
μμ λ λ³μμ λν΄ super λ‘ μ°Έμ‘°νλλ°, λ©μλμ λν΄μλ super λ‘ μ°Έμ‘°ν μ μλ€.
super()
μμ±μμμ λ€λ₯Έ μμ±μλ₯Ό νΈμΆ ν λ this() λ₯Ό μ¬μ©νμλ€. super() λ μμ±μλ₯Ό νΈμΆν λ μ¬μ©ν μ μλ€. λΆλͺ¨ ν΄λμ€μ μμ±μλ₯Ό νΈμΆνλ λ°©λ²μ΄λ€.
μμ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό μμ±νλ©΄, λΆλͺ¨ + μμμ΄ λ νλμ μΈμ€ν΄μ€κ° μμ±λλ κ²μ΄κΈ° λλ¬Έμ, μμ ν΄λμ€μ μμ±μκ° νΈμΆλ λ λΆλͺ¨ ν΄λμ€μ μμ±μλ ν¨κ» νΈμΆμ΄ λμ΄μΌ νλ€.
μ΄λ μμ ν΄λμ€μμ λΆλͺ¨ ν΄λμ€μ μμμ μ¬μ©ν μ μκΈ° λλ¬Έμ μμ ν΄λμ€ μμ±μμ 첫 μ€μμλ λΆλͺ¨ ν΄λμ€μ μμ±μλ₯Ό νΈμΆν΄μΌ νλ€. λ§μ½ 첫 μ€μμ super() λ₯Ό νΈμΆνμ§ μμλ€λ©΄ μ»΄νμΌλ¬κ° μλμΌλ‘ μΆκ°ν΄μ€λ€.
λ§μ½ λΆλͺ¨ ν΄λμ€μ κΈ°λ³Έ μμ±μκ° μλ κ²½μ°μ μ»΄νμΌλ¬κ° μμ μμ±μμμ super(); λ₯Ό μΆκ°νλ©΄ μλ¬κ° λ°μν μ μκΈ° λλ¬Έμ μ£Όμν΄μΌ νλ€. μλμ κ°μ κ²½μ°μ΄λ€.
class Product {
String productCode;
String productType;
public Product(String productCode, String productType) {
this.productCode = productCode;
this.productType = productType;
}
}
class Book extends Product {
String author;
String title;
public Book(String productCode, String productType,
String author, String title) {
this.author = author;
this.title = title;
}
}
μμ μ½λλ λΆλͺ¨ ν΄λμ€μλ κΈ°λ³Έ μμ±μκ° μλλ°, μμ μμ±μμμ λΆλͺ¨ μμ±μλ₯Ό νΈμΆνμ§ μμκΈ° λλ¬Έμ μλ¬κ° λ°μνλ€.
class Book extends Product {
String author;
String title;
public Book(String productCode, String productType, String author, String title) {
super(productCode, productType); // μΆκ°!
this.author = author;
this.title = title;
}
}
λΆλͺ¨ ν΄λμ€μ μμ±μλ₯Ό νΈμΆν΄μ€μΌ λ¬Έμ μμ΄ μΈμ€ν΄μ€λ₯Ό μμ±ν μ μλ€.
μ€λ²λΌμ΄λ©, Overriding
λΆλͺ¨ ν΄λμ€μμ μμλ°μ λ©μλλ₯Ό λ€μ μμ±νλ κ²μ μ€λ²λΌμ΄λ© μ΄λΌκ³ νλ€.
class Product {
String productCode;
String productType;
String getDisplayData() {
return String.format("Product Code: %s, Product Type: %s", productCode, productType);
}
}
class Book extends Product {
String author;
String title;
@Override
String getDisplayData() {
return String.format("Product Type: %s, Book Title: %s, Book author: %s",
productType, title, author);
}
}
Product ν΄λμ€λ₯Ό μμλ°μ getDisplayData λ©μλλ₯Ό μ€λ²λΌμ΄λ© νλ€.
μ€λ²λΌμ΄λ©μ νλ€λ κ²μ λ©μλμ Body λΆλΆλ§ μ¬μμ± νλ κ²μ΄κΈ° λλ¬Έμ μ μΈλΆλ μμ ν λμΌν΄μΌ νλ€. λ©μλ λͺ
, λ§€κ°λ³μ, λ°ν νμ
μ΄ λμΌν΄μΌ νλ€. λλΆλΆμ κ²½μ° λΆλͺ¨ ν΄λμ€μ μ μΈλΆμ λκ°μ΄ μ¬μ©νμ§λ§ μ κ·Ό μ μ΄μμ μμΈλ 쑰건μ μΌλ‘ λ³κ²½ν μ μλ€.
λΆλͺ¨ ν΄λμ€μ λ©μλλ³΄λ€ λμ λ²μμ μ κ·Ό μ μ΄μλ‘ λ³κ²½ν μ μλ€. μ κ·Ό μ μ΄μμ λ²μλ public -> protected -> default -> private μμΌλ‘ μ’νμ§λ€. μλ₯Ό λ€λ©΄ λΆλͺ¨ ν΄λμ€μ λ©μλκ° protected μμΌλ©΄ μμ ν΄λμ€λ λκ°μ΄ protected μ΄κ±°λ, public μ΄μ΄μΌ νλ€λ κ²μ΄λ€. private λ‘ λ³κ²½νλ©΄ μ»΄νμΌλ¬κ° μλ¬λ₯Ό μλ €μ€λ€.
κ·Έλ¦¬κ³ λΆλͺ¨ ν΄λμ€μ λ©μλλ³΄λ€ λ§μ μμ μμΈλ₯Ό μ μΈν μ μλ€. μλ₯Ό λ€μ΄ λΆλͺ¨ λ©μλκ° IOException, IllegalArgumentException μμΈλ₯Ό λμ§λ κ²½μ° μμμμλ λ μ€ νλμ μμΈλ§ λμ Έλ λλ€. νμ§λ§ λ€λ₯Έ μμΈλ₯Ό λμ§κ±°λ νλ©΄ μμ μλ¬κ° μκΈ΄λ€.
μ μ΄μ, Modifier
μ μ΄μλ ν΄λμ€, λ³μ, λ©μλμ μ μΈλΆμ μ¬μ©λμ΄ λΆκ°μ μΈ μλ―Έλ₯Ό λΆμ¬νλ€. ν¬κ² μ κ·Ό μ μ΄μμ μ κ·Ό μ μ΄μλ₯Ό μ μΈν μ μ΄μλ€μ΄ μλ€.
μ κ·Ό μ μ΄μ: public, protected, default, private
κ·Έ μΈ μ μ΄μ: static, final, abstract, native, transient, synchronized, volatile, strictfp
μ κ·Ό μ μ΄μλ ν λ²μ νλλ§ μ¬μ©ν μ μκ³ , κ·Έ μΈμ μ μ΄μλ€μ μ¬λ¬κ°μ§λ₯Ό μ‘°ν©ν΄μ μ¬μ©ν μ μλ€.
public static final int NUMBER = 10;
μ΄λ κ² μ¬μ©ν μλ μμ§λ§, public private static μ΄λ κ² public κ³Ό private μ ν¨κ» μ¬μ©ν μλ μλ€.
μ μ΄μλ€μ μ‘°ν©ν΄μ μ¬μ©ν λ, μ μ΄μμ μμλ μκ΄μ΄ μλ€. λ³΄ν΅ μ κ·Ό μ μ΄μλ₯Ό κ°μ₯ μΌμͺ½μ λκ³ , κ·Έ μΈμ μ μ΄μλ€μ μ μ νκ² μμ±νλ©΄ λλ€.
μ κ·Ό μ μ΄μ
μ κ·Ό μ μ΄μλ ν΄λμ€, λ³μ, λ©μλμ μ μ©ν΄μ μΈλΆμμ μ κ·Όνμ§ λͺ»νλλ‘ μ ννλ μν μ νλ€.
μΈλΆλ‘λΆν° λ°μ΄ν°λ₯Ό 보νΈνκΈ° μν΄μ, λΆνμν λΆλΆμ κ°μΆκΈ° μν΄μ μ¬μ©νλ κ²μ΄λ€. κ°μ²΄μ§ν₯κ°λ
μ μΊ‘μν (Encapsulation)μ ν΄λΉνλ€.
ν΄λμ€μλ public, default μ μ΄μκ° νμ©λκ³ ,
λ³μμ λ©μλμλ public, protected, default, private μ μ΄μκ° νμ©λλ€.
μ κ·Ό μ μ΄μλ₯Ό λ°λ‘ μ μνμ§ μλ κ²½μ°, default μ μ΄μκ° κΈ°λ³ΈμΌλ‘ μ μ©λλ€.
public | μ κ·Ό μ νμ΄ μλ€. |
protected | κ°μ ν¨ν€μ§ λ΄μμ, λ€λ₯Έ ν¨ν€μ§μΈ κ²½μ° μμ ν΄λμ€μμ μ κ·Όμ΄ κ°λ₯νλ€. |
default | κ°μ ν¨ν€μ§ λ΄μμ μ κ·Όμ΄ κ°λ₯νλ€. |
private | κ°μ ν΄λμ€ λ΄μμλ§ μ κ·Όμ΄ κ°λ₯νλ€. |
final
final μ μ΄μλ ν΄λμ€, λ©μλ, λ³μ λͺ¨λμ λΆμΌ μ μλ€.
ν΄λμ€μ λΆμ΄λ©΄ λ³κ²½, νμ₯ν μ μλ ν΄λμ€κ° λκ³ , λ©μλμ λΆμ΄λ©΄ μ€λ²λΌμ΄λ©μ ν΅ν΄ μ¬μ μ λ μ μκ³ , λ³μμ λΆμ΄λ©΄ κ°μ λ³κ²½ν μ μλ μμκ° λλ€.
final class FinalClass {
final int MAX_VALUE = 1_000;
final void getMaxValue() {
return MAX_VALUE;
}
}
final μ΄ λΆμ λ³μλ μμκ° λκΈ° λλ¬Έμ μΌλ°μ μΌλ‘ μ μΈκ³Ό λμμ μ΄κΈ°νλ₯Ό νλ€. νμ§λ§ μΈμ€ν΄μ€ λ³μμ λΆμ final λ³μλ μμ±μμμ μ΄κΈ°ννλλ‘ ν μ μλ€.
λ§€κ°λ³μλ₯Ό λ°λ μμ±μλ₯Ό μμ±ν΄μ μΈμ€ν΄μ€λ₯Ό μμ±ν λ final μ΄ λΆμ λ³μλ₯Ό μ΄κΈ°ν νλλ° νμν κ°μ μ 곡λ°μ μ΄κΈ°ν νλ κ²μ΄λ€. μ΄λ κΈ°λ³Έ μμ±μκ° μμΌλ©΄ μλ¬κ° λ°μνλ€.
class FinalClass {
final int constValue;
public FinalClass(int constValue) {
this.constValue = constValue;
}
}
μμμ λ§λ λ κ°μ§ λ°©λ²
λ κ°μ§ λ°©λ² μ€ νλλ λ°λ‘ μμμ λ΄€λ final μ class μ λΆμ΄λ κ²μ΄λ€. λ λ€λ₯Έ λ°©λ²μ ν΄λμ€μ μμ±μμ private μ λΆμ΄λ κ²μ΄λ€.
μμ ν΄λμ€μμ λΆλͺ¨ ν΄λμ€μ μμ±μλ₯Ό νΈμΆν΄μΌ νλλ° λΆλͺ¨ ν΄λμ€ μμ±μμ μ κ·Ό μ μ΄μκ° private μ΄κΈ° λλ¬Έμ μμ ν΄λμ€μμ μ κ·Όμ΄ λΆκ°λ₯ν κ²μ΄λ€.
class FinalClass {
private FinalClass() {
}
}
νμ§λ§ μ΄λ° κ²½μ° λͺ
μμ μΌλ‘ class μμ final μ μΆκ°νλ κ²μ΄ λ μ’κ² λ€.
μμ±μκ° private μΈ κ²½μ° static public λ©μλλ₯Ό μμ±ν΄μ μΈμ€ν΄μ€λ₯Ό μμ±ν΄μ£Όλ μμ
μ νλ€. μΈμ€ν΄μ€μ κ°μλ₯Ό μ ννκ±°λ ν λ μ΄λ° λ°©λ²μ μ¬μ©νλ€.
λ€νμ±, Polymorphism
κ°μ²΄μ§ν₯κ°λ μμ λ€νμ±μ 'μ¬λ¬ κ°μ§ ννλ₯Ό κ°μ§ μ μλ λ₯λ ₯'μ μλ―Ένλ€. Java μμλ λ€νμ±μ λΆλͺ¨ ν΄λμ€μ μ°Έμ‘° λ³μλ‘ μμ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό μ°Έμ‘°ν μ μλλ‘ κ΅¬ννλ€.
class Product {
private String productCode;
private String productType;
}
class Book extends Product {
private String author;
private String title;
}
public class SampleTest {
public static void main(String[] args) {
Product bookProduct = new Book();
}
}
bookProduct λ Product λ₯Ό μ°Έμ‘°ν μ μλ μ°Έμ‘°λ³μμ΄μ§λ§, Product ν΄λμ€λ₯Ό μμ λ°μ Book ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό μ°Έμ‘°ν μ μλ€.
μΈμ€ν΄μ€λ₯Ό λΆλͺ¨ νμ
μ μ°Έμ‘° λ³μλ‘ μ°Έμ‘°νλ κ²κ³Ό, μμ νμ
μ μ°Έμ‘° λ³μλ‘ μ°Έμ‘°νλ κ²μ μ¬μ©ν μ μλ μμμ λ²μμμ μ°¨μ΄κ° μκΈ΄λ€.
class Product {
private String productCode;
private String productType;
void showCode() {}
void showType() {}
}
class Book extends Product {
private String author;
private String title;
void showAuthor() {}
void showTitle() {}
}
public class SampleTest {
public static void main(String[] args) {
Product bookProduct = new Book();
bookProduct.showCode();
bookProduct.showType();
// bookProduct.showAuthor(); μλ¬!
}
}
Product νμ
μ μ°Έμ‘°λ³μλ‘λ λΆλͺ¨ ν΄λμ€μμ μ μλμ΄ μλ μμλ€μ νν΄μ μ κ·Όν μ μλ€.
μμ ν΄λμ€μ λ νΌλ°μ€λ‘λ λΉμ°ν Book μΈμ€ν΄μ€μ λͺ¨λ μμμ μ κ·Όμ΄ κ°λ₯νλ€.
λ°λλ‘ μμ νμ μ μ°Έμ‘° λ³μλ‘ λΆλͺ¨ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό μ°Έμ‘°ν μλ μλ€.
Book book = new Product(); // μλ¬!
μ°Έμ‘° λ³μμ ν λ³ν
μμμ Product μ μ°Έμ‘° λ³μλ‘λ Book μΈμ€ν΄μ€μ λ©μλλ₯Ό μ¬μ©νμ§ λͺ»νλλ°, μ°Έμ‘° λ³μλ₯Ό ν λ³ν ν΄μ ν΄κ²° ν μ μλ€.
public static void main(String[] args) {
Product bookProduct = new Book();
bookProduct.showType();
Book book = (Book) bookProduct;
book.showAuthor();
}
μλ‘ μμ κ΄κ³μ μλ ν΄λμ€ μ¬μ΄μμλ§ κ°λ₯νλ€. μμ νμ
μ μ°Έμ‘° λ³μλ₯Ό λΆλͺ¨ νμ
μ μ°Έμ‘° λ³μλ‘, λΆλͺ¨ νμ
μ μ°Έμ‘° λ³μλ₯Ό μμ νμ
μ μ°Έμ‘° λ³μλ‘ νλ³νμ΄ κ°λ₯νλ€.
μμ νμ
μ μ°Έμ‘° λ³μλ₯Ό λΆλͺ¨ νμ
μ μ°Έμ‘° λ³μλ‘ νλ³ν νλ κ²μ Up-Casting
λΆλͺ¨ νμ
μ μ°Έμ‘° λ³μλ₯Ό μμ νμ
μ μ°Έμ‘° λ³μλ‘ νλ³ν νλ κ²μ Down-Casting λΌκ³ νλ€.
μ¬κΈ°μ Up-Casting ν λλ νλ³ν μ°μ°μλ₯Ό μλ΅ν μ μλ€.
public static void main(String[] args) {
Product bookProduct = new Book();
bookProduct.showType();
Book book = (Book) bookProduct; // (Book) μλ΅ λΆκ°
book.showAuthor();
Book book2 = new Book();
Product bookProduct2 = book2; // (Product) μλ΅ κ°λ₯
}
νλ³ν μ μ£Όμν κ²
public static void main(String[] args) {
Product product = new Product();
Book book = (Book) product;
}
μμ κ°μ΄ νλ³ν νλ©΄ λ°νμ μ ClassCastException μμΈκ° λ°μνλ€.
λ¬Έμ λ product κ° μ°Έμ‘°νλ μΈμ€ν΄μ€κ° Product νμ
μ΄κΈ° λλ¬Έμ Book νμ
μ μ°Έμ‘°λ³μλ‘ νλ³ν ν μ μλ€.
μ°Έμ‘° λ³μκ° μ¬μ©ν μ μλ μμμ μλ μΈμ€ν΄μ€μ μμ μλ³΄λ€ κ°κ±°λ μ μ΄μΌ νλλ°, Book μ°Έμ‘°λ³μλ‘λ Product μ°Έμ‘° λ³μκ° μ°Έμ‘°ν μ μλ μμλ³΄λ€ λ λ§μ μ μκΈ° λλ¬Έμ λ¬Έμ κ° λ°μνλ κ²μ΄λ€.
κ·Έλμ νμ μ°Έμ‘°λ³μλ₯Ό νλ³ν ν λμλ instanceof μ°μ°μλ₯Ό μ¬μ©ν΄μ μΈμ€ν΄μ€μ νμ
μ΄ λ¬΄μμΈμ§ νμΈνλ μμ
μ΄ μ€μνλ€.
public static void main(String[] args) {
Product product = new Product();
if (product instanceof Book) {
Book book = (Book) product;
}
}
Dynamic Method Dispatch
Dynamic Method Dispatch λ μ€λ²λΌμ΄λ© λ λ©μλλ₯Ό νΈμΆν λ μ΄λ€ λ©μλλ₯Ό νΈμΆν μ§μ λν κ²°μ μ μ»΄νμΌ μμ μ΄ μλλΌ λ°νμ μμ μ κ²°μ νλ λ©μ»€λμ¦μ λ»νλ€.
λ§λΆμ¬μ μ€λͺ
νλ©΄, λΆλͺ¨ ν΄λμ€μ μ°Έμ‘° λ³μλ‘ μ€λ²λΌμ΄λ© λ λ©μλλ₯Ό νΈμΆνλ €κ³ νλ κ²½μ°μ μ°Έμ‘°λλ μΈμ€ν΄μ€μ νμ
μ κΈ°μ€μΌλ‘ μ΄λ€ λ©μλλ₯Ό μ€νν μ§λ₯Ό κ²°μ νλ κ²μ΄λ€. μΈμ€ν΄μ€μ νμ
μ λ³΄κ³ κ²°μ νκΈ° λλ¬Έμ λ°νμ μμ μ κ²°μ νλ€λ κ²μ΄λ€.
class Parent {
void show() {
System.out.println("Parent - show method is called.");
}
}
class ChildA extends Parent {
void show() {
System.out.println("Child A - show method is called.");
}
}
class ChildB extends Parent {
void show() {
System.out.println("Child B - show method is called.");
}
}
public class SampleTest {
public static void main(String[] args) {
Parent parent = new Parent();
ChildA childA = new ChildA();
ChildB childB = new ChildB();
Parent ref = null;
ref = parent;
ref.show(); // Parent - show method is called.
ref = childA;
ref.show(); // Child A - show method is called.
ref = childB;
ref.show(); // Child B - show method is called.
}
}
show() λ©μλλ νΈμΆ μμ μ°Έμ‘°λλ μΈμ€ν΄μ€μ νμ
μ λ°λΌμ λΆλͺ¨μ show() νΉμ μμμ show() μ€ μ΄λ€ λ©μλλ₯Ό νΈμΆν μ§ κ²°μ νλ κ²μ΄λ€.
Dynamic Method Dispatch λ λ©€λ² λ³μμ λν΄μλ μ μ©λμ§ μλλ€. μ΄λ€ μΈμ€ν΄μ€μΈμ§κ° μλλΌ μ΄λ€ μ°Έμ‘° λ³μλ₯Ό μ¬μ©νκ³ μλμ§λ₯Ό λ³΄κ³ κ²°μ λλ€.
class Parent {
int num = 100;
}
class ChildA extends Parent {
int num = 200;
}
public class SampleTest {
public static void main(String[] args) {
Parent p = new ChildA();
ChildA c = new ChildA();
System.out.println("Parent Ref: " + p.num); // Parent Ref: 100
System.out.println("Child Ref: " + c.num); // Child Ref: 200
}
}
μΆμ ν΄λμ€
μΆμ ν΄λμ€λ μΆμ λ©μλλ₯Ό κ°μ§ ν΄λμ€λ₯Ό λ»νλ€. μΆμ λ©μλλ μ μ΄μμμ λ΄€λ abstract ν€μλλ₯Ό μ¬μ©ν΄ λ§λ€ μ μλ€.
abstract class AbstractClass {
abstract void doSomething();
}
μΆμ λ©μλκ° μλ ν΄λμ€μ abstract μ μ΄μκ° μμΌλ©΄ μλ¬κ° λ°μνλ€.
μΆμ λ©μλλ μμ§ μμ±λμ§ μμ λ©μλλ₯Ό λ»νκ³ , μΆμ ν΄λμ€μμ μΆμ λ©μλλ μ μΈλΆλ§ μ‘΄μ¬νλ€. μμμλ doSomething(); μ΄λ κ² λλκ±Έ λ³Ό μ μλ€.
μΆμ ν΄λμ€λ₯Ό μμλ°λ μμ ν΄λμ€μμ ν΄λΉ μΆμ λ©μλμ μ μλ₯Ό κ°μ νλ€. μ΄ κ°μ μ΄μ λ‘ μΆμ ν΄λμ€λ₯Ό μΈμ€ν΄μ€λ‘ μμ±ν μ μλ€. μ μλμ§ μλ λ©μλκ° μλ μΈμ€ν΄μ€λ₯Ό λ§λ€ μλ μκΈ° λλ¬Έμ΄λ€.
abstract class Machine {
abstract void run();
abstract void stop();
}
class Tv extends Machine {
@Override
void run() {
System.out.println("Tv Run");
}
@Override
void stop() {
System.out.println("Tv Stop");
}
}
public class SampleTest {
public static void main(String[] args) {
// Machine m = new Machine(); μλ¬!
Tv tv = new Tv();
Machine tv2 = new Tv(); // μΆμ ν΄λμ€λ λ€νμ± μ μ©μ΄ κ°λ₯νλ€.
}
}
μΆμ ν΄λμ€λ μΆμ λ©μλκ° μλ€λ κ² λ§κ³ λ μΌλ° ν΄λμ€μ λ€λ₯Ό κ²μ μλ€. λκ°μ΄ λ³μ, λ©μλλ₯Ό μμ±ν μ μλ€.
Object ν΄λμ€
Object ν΄λμ€λ λͺ¨λ ν΄λμ€μ μ΅κ³ μ‘°μμ΄ λλ ν΄λμ€μ΄λ€. λͺ¨λ ν΄λμ€λ μ¬μ€ Object ν΄λμ€λ₯Ό μμλ°κ³ μλ κ²μ΄λ€. λ°λ‘ extends ν΄μ μμ λ°λ ν΄λμ€κ° μμλ€λ©΄ μλμΌλ‘ Object ν΄λμ€λ₯Ό μμ λ°κ²λλ€.
Object ν΄λμ€λ java.lang ν¨ν€μ§μ μλλ° μλ° νλ‘κ·Έλλ°μμ κ°μ₯ κΈ°λ³Έμ΄ λλ ν΄λμ€λ€μ ν¬ν¨νκ³ μλ€. java.lang νμμ ν΄λμ€λ€μ λ°λ‘ import νμ§ μμλ μ¬μ©μ΄ κ°λ₯νλ€.
Object ν΄λμ€λ λͺ¨λ ν΄λμ€μ μ‘°μμ΄ λκΈ° λλ¬Έμ Object ν΄λμ€μ λ©€λ²λ€μ λͺ¨λ ν΄λμ€μμ μ¬μ©μ΄ κ°λ₯νλ€.
Object ν΄λμ€λ λ©€λ² λ³μλ μκ³ , 11 κ°μ λ©μλλ§ κ°μ§κ³ μλ€.
clone() | κ°μ²΄ μμ μ 볡μ¬λ³Έ λ°ν |
equals() | κ°μ²΄ μμ κ³Ό κ°μ κ°μ²΄μΈμ§ boolean νμ μΌλ‘ λ°ν |
finalize() | κ°μ²΄κ° μλ©Έλ λ GC μ μν΄ μλμ μΌλ‘ νΈμΆ, μλ©Έμ μνν΄μΌν μμ μ΄ μμΌλ©΄ μ¬μ μ νλ©΄ λλ€. (κ±°μ μ¬μ©νμ§ μμ) |
getClass() | κ°μ²΄μ ν΄λμ€ μ 보λ₯Ό λ΄κ³ μλ Class μΈμ€ν΄μ€ λ°ν |
hashCode() | κ°μ²΄μ hash code λ°ν |
toString() | κ°μ²΄μ μ 보λ₯Ό λ¬Έμμ΄λ‘ λ°ν |
notify() | κ°μ²΄λ₯Ό μ¬μ©νλ €λ Thread λ₯Ό νλλ§ κΉ¨μ΄λ€. |
notifyAll() | κ°μ²΄λ₯Ό μ¬μ©νλ €λ λͺ¨λ Thread λ₯Ό κΉ¨μ΄λ€. |
wait() | λ€λ₯Έ Thread κ° notify(), notifyAll() λ₯Ό νΈμΆν λκΉμ§ νμ¬ Thread λ₯Ό κΈ°λ€λ¦¬κ² νλ€. 무ννκ² νΉμ μ§μ λ μκ°λμ κΈ°λ€λ¦¬κ² ν μ μλ€. (timeout μ 1/1000 μ΄, nanos λ 1/10^9 μ΄) |
wait(long timeout)() | |
wait(long timeout, int nanos) |
equals(Object obj)
λ§€κ°λ³μλ‘ μ°Έμ‘° λ³μλ₯Ό λ°μμ κ°μ μ°Έμ‘° λ³μμΈμ§ κ²°κ³Όλ₯Ό boolean νμ
μΌλ‘ μλ €μ€λ€.
μ£Όμ κ°μ΄ μλλΌ μ€μ μΈμ€ν΄μ€μ λ΄μ©μ λΉκ΅νκ³ μΆμ κ²½μ° equals λ₯Ό μ¬μ μνλ€. String ν΄λμ€μ equals() λ©μλλ μ€μ λ¬Έμμ΄ κ°μ λΉκ΅νλλ‘ λμ΄μλ€.
hashCode()
hashCode() λ©μλλ κ°μ²΄μ μ μΌν ν΄μ μ½λ κ°μ λ§λ€μ΄ λ°ννλ€. Object ν΄λμ€μ hashCode() λ©μλλ κ°μ²΄μ μ£Όμ κ°μ μ΄μ©νλ€.
κ°μ ν΄μμ½λκ° μ‘΄μ¬ν μ μμ§λ§ Object μ hashCode() λ©μλλ κ°μ²΄μ μ£Όμ κ°μ μ¬μ©ν΄μ μ½λλ₯Ό λ§λ€κΈ° λλ¬Έμ λ κ°μ²΄λ κ²°μ½ κ°μ κ°μ κ°μ§ μ μλ€. μΈμ€ν΄μ€ λ΄μ©μ κ°μ§κ³ ν΄μ μ½λλ₯Ό λ§λ€κ³ μΆλ€λ©΄ μ¬μ μν΄μΌνλ€.
μΈμ€ν΄μ€ κ°μΌλ‘ λ κ°μ²΄λ₯Ό λΉκ΅νκ³ μΆλ€λ©΄ equals, hashCode λκ°μ§ λ©μλλ₯Ό λͺ¨λ μ μ ν μ¬μ μν΄μΌνλ€.
toString()
μΈμ€ν΄μ€μ λν μ 보λ₯Ό λ¬Έμμ΄λ‘ μ 곡νλ€. μ¬μ μ νμ§ μμΌλ©΄ ν΄λμ€ μ΄λ¦κ³Ό 16μ§μ ν΄μ μ½λκ° λ°νλλ€. μ μ ν μ¬μ μ νλ κ²μ΄ μ’λ€.
References
https://www.geeksforgeeks.org/dynamic-method-dispatch-runtime-polymorphism-java/
μλ°μ μ μ (www.yes24.com/Product/Goods/24259565)