λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Programming/Java

15 Inheritance

by Dowon Kang 2024. 1. 20.

상속은 κ°μ²΄μ§€ν–₯ ν”„λ‘œκ·Έλž˜λ°(OOP)의 μ€‘μš”ν•œ κ°œλ… μ€‘ ν•˜λ‚˜μž…λ‹ˆλ‹€. μƒμ†μ€ μ½”λ“œμ˜ μž¬μ‚¬μš©μ„±μ„ λ†’이고 ν΄λž˜μŠ€ κ°„μ˜ κ³„μΈ΅ κ΅¬μ‘°λ₯Ό μ •μ˜ν•˜λŠ” λ° μ‚¬μš©λ©λ‹ˆλ‹€. κΈ°μ‘΄ ν΄λž˜μŠ€μ˜ νŠΉμ„±μ„ μ΄μ–΄λ°›μ•„ μƒˆλ‘œμš΄ ν΄λž˜μŠ€λ₯Ό λ§Œλ“€ μˆ˜ μžˆκ²Œ ν•΄μ£Όλ©°, μ½”λ“œμ˜ μΌκ΄€μ„±μ„ μœ μ§€ν•˜κ³  μœ μ§€λ³΄μˆ˜λ₯Ό μš©μ΄ν•˜κ²Œ λ§Œλ“­λ‹ˆλ‹€.

 

μƒμ†μ˜ 4가지 νŠΉμ§• 

  • μžλ°”λŠ” 단일 μƒμ†λ§Œμ„ μ§€μ›ν•©λ‹ˆλ‹€.
  • μ΅œμƒμœ„ 쑰상은 Object ν΄λž˜μŠ€μž…λ‹ˆλ‹€.
  • μžμ† ν΄λž˜μŠ€λŠ” μ‘°μƒμœΌλ‘œλΆ€ν„° λͺ¨λ“  멀버λ₯Ό μƒμ†λ°›μŠ΅λ‹ˆλ‹€.
  • μžμ† 클래슀의 λ©€λ²„λŠ” 항상 쑰상보닀 κ°™κ±°λ‚˜ 더 λ§Žμ€ 멀버λ₯Ό κ°–μŠ΅λ‹ˆλ‹€.

 

 

λ©”μ„œλ“œ μ˜€λ²„라이딩(Method Overriding)

λ©”μ„œλ“œ μ˜€λ²„라이딩(Method Overriding)은 μžμ‹ ν΄λž˜μŠ€μ—μ„œ λΆ€λͺ¨ ν΄λž˜μŠ€μ˜ λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜ν•˜λŠ” κ°œλ…μž…λ‹ˆλ‹€. μ¦‰, λΆ€λͺ¨ ν΄λž˜μŠ€μ—μ„œ μ΄λ―Έ μ •μ˜λœ λ©”μ„œλ“œλ₯Ό μžμ‹ ν΄λž˜μŠ€μ—μ„œ κ°™μ€ μ‹œκ·Έλ‹ˆμ²˜(λ©”μ„œλ“œ μ΄λ¦„, λ§€κ°œλ³€μˆ˜ λͺ©λ‘, λ°˜ν™˜ νƒ€μž…)둜 λ‹€μ‹œ μ •μ˜ν•˜μ—¬ μ‚¬μš©ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄ μžμ‹ ν΄λž˜μŠ€μ—μ„œ λΆ€λͺ¨ ν΄λž˜μŠ€μ˜ λ©”μ„œλ“œλ₯Ό μƒˆλ‘œ κ΅¬ν˜„ν•˜κ±°λ‚˜ κΈ°λŠ₯을 ν™•μž₯ν•  μˆ˜ μžˆμŠ΅λ‹ˆλ‹€.

μ˜€λ²„λΌμ΄λ”©μ„ μœ„ν•œ λͺ‡ 가지 κ·œμΉ™

 

  1. μ˜€λ²„λΌμ΄λ”©ν•˜λ €λŠ” λ©”μ„œλ“œλŠ” λΆ€λͺ¨ ν΄λž˜μŠ€μ—μ„œ μ„ μ–Έλ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€.
  2. μ˜€λ²„λΌμ΄λ”©ν•˜λ €λŠ” λ©”μ„œλ“œμ˜ μ‹œκ·Έλ‹ˆμ²˜(이름, λ§€κ°œλ³€μˆ˜ λͺ©λ‘, λ°˜ν™˜ νƒ€μž…)λŠ” λΆ€λͺ¨ ν΄λž˜μŠ€μ˜ λ©”μ„œλ“œμ™€ λ™μΌν•΄μ•Ό ν•©λ‹ˆλ‹€.
  3. μ ‘κ·Ό μ œμ–΄μžλŠ” λΆ€λͺ¨ ν΄λž˜μŠ€μ˜ λ©”μ„œλ“œλ³΄λ‹€ μ’μ€ λ²”μœ„λ‘œ λ³€κ²½ν•  μˆ˜ μ—†μŠ΅λ‹ˆλ‹€.
  4. μ˜ˆμ™ΈλŠ” λΆ€λͺ¨ ν΄λž˜μŠ€μ˜ λ©”μ„œλ“œλ³΄λ‹€ λ” μ κ±°λ‚˜ κ°™μ€ μˆ˜μ˜ μ˜ˆμ™Έλ₯Ό λ˜μ§ˆ μˆ˜ μžˆμŠ΅λ‹ˆλ‹€.

 

 

super & super()

superλŠ” μžλ°”μ—μ„œ ν‚€μ›Œλ“œλ‘œ μ‚¬μš©λ˜λ©°, μ£Όλ‘œ μƒμ† κ΄€κ³„μ—μ„œ λΆ€λͺ¨ ν΄λž˜μŠ€μ˜ λ©€λ²„에 μ ‘κ·Όν•˜λŠ” λ°μ— μ‚¬μš©λ©λ‹ˆλ‹€. 

 

1) λΆ€λͺ¨ 클래슀의 멀버 호좜: μžμ‹ ν΄λž˜μŠ€μ—μ„œ 같은 μ΄λ¦„μ˜ 멀버가 λΆ€λͺ¨ ν΄λž˜μŠ€μ—λ„ μ‘΄μž¬ν•  λ•Œ, super ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ λΆ€λͺ¨ 클래슀의 멀버λ₯Ό μ°Έμ‘°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

2) λΆ€λͺ¨ 클래슀의 μƒμ„±μž 호좜: μžμ‹ 클래슀의 μƒμ„±μžμ—μ„œ λΆ€λͺ¨ 클래슀의 μƒμ„±μžλ₯Ό ν˜ΈμΆœν•  λ•Œ super()λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. 이것은 주둜 μžμ‹ 클래슀의 μƒμ„±μžμ—μ„œ λΆ€λͺ¨ 클래슀의 μ΄ˆκΈ°ν™”λ₯Ό μˆ˜ν–‰ν•˜κΈ° μœ„ν•΄ μ‚¬μš©λ©λ‹ˆλ‹€.

 

 

상속 μ½”λ“œ μ˜ˆμ‹œ ( 상속, λ©”μ„œλ“œ μ˜€λ²„λΌμ΄λ”©, super, super() ) 

더보기

public class _02_Inheritance {
    public static void main(String[] args) {

        Car car = new Car();;
        car.setYear(2022);
        car.display();

        Motorcycle motorcycle = new Motorcycle();
        motorcycle.setModel("Sports");
        motorcycle.setPrice(300000);
        motorcycle.display();
    }
}

 

 

더보기

// Parent class
class Vehicle {
    protected String brand;

    public Vehicle(String brand) {
        this.brand = brand;
    }

    public void display() {
        System.out.print("Brand: " + brand + ", ");
    }
}

 

 

더보기

// Child class inheriting from Vehicle
class Car extends Vehicle {
    private int year;

    public Car() {
        super("KIA"); // super() : invoke the constructor of parent
    }
    public void setYear(int year) {
        this.year = year;
    }

    @Override
    public void display() {
        super.display(); // super : call parent class
        System.out.println("Year: " + year);
    }
}

 

 

더보기

// Child class inheriting from Vehicle
class Motorcycle extends Vehicle {
    private String model;
    private int price;

    public Motorcycle() {
        super("Harley-Davidson");
    }
    public void setModel(String model) {
        this.model = model;
    }
    public void setPrice(int price) {
        this.price = price;
    }

    // Method overriding
    @Override
    public void display() {
        super.display();

        System.out.print("Model: " + model + ", ");

        System.out.print("Price: " + price);
    }
}

 

 

2번 μ§Έ μ˜ˆμ‹œ μ½”λ“œ
더보기
public class Inheritance_Practice {
    public static void main(String[] args) {

        Dog d = new Dog(3, "Female");
        d.printName();
    }
}

class Animal_Park {
    String name = "Animal";
    int age;

    public Animal_Park(int age) {
        this.age = age;
    }
    public void printName() {}
}

class Dog extends Animal_Park {
    String name = "Dog";
    String sex;

    public Dog(int age, String sex) {
        super(age);
        this.sex = sex;
    }

    @Override
    public void printName() {
        // λΆ€λͺ¨ ν΄λž˜μŠ€μ˜ λ©€λ²„에 μ ‘κ·Ό
        System.out.println(super.name + " : " + name);
        System.out.println("Age : " + age);
        System.out.println("Sex : " + sex);
    }
}

 

 



Inheritance: OOP concept where a class inherits properties and behaviors from another class, promoting code reuse and establishing a hierarchy.

Method Overriding: OOP feature allowing a subclass to provide a specific implementation for a method already defined in its superclass.

super: Keyword in Java used to refer to superclass members, accessing methods and fields from the parent class in the subclass.

super(): Used to invoke the superclass constructor in the subclass, often as the first statement in the subclass constructor.

 

'Programming > Java' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

17 Abstraction  (0) 2024.01.20
16 Composition relationship  (0) 2024.01.20
14 Encapsulation (feat. Access modifier)  (0) 2024.01.20
13 Method  (0) 2024.01.20
12 Constructor  (0) 2024.01.19

λŒ“κΈ€