[STUDY] 231213 | Java | 상속(Inheritance)

NimgnosΒ·2023λ…„ 12μ›” 14일
0

πŸ‘Ύ STUDY

λͺ©λ‘ 보기
18/40
post-thumbnail

πŸ’» 상속(Inheritance)

  • λΆ€λͺ¨ 클래슀(슈퍼, μƒμœ„ 클래슀)와 μžμ‹ 클래슀(μ„œλΈŒ, ν•˜μœ„ 클래슀)κ°€ 있으며, μžμ‹ ν΄λž˜μŠ€λŠ” λΆ€λͺ¨ 클래슀λ₯Ό μ„ νƒν•΄μ„œ, λΆ€λͺ¨ ν΄λž˜μŠ€μœ„ λ³€μˆ˜μ™€ λ©”μ†Œλ“œλ₯Ό 상속받아 μ“Έ 수 있음.
  • μžλ°”μ—μ„œ 닀쀑 상속은 λ¬Έλ²•μ μœΌλ‘œ λΆˆκ°€ν•¨.(단계적인 상속은 κ°€λŠ₯)

πŸ“Œ 상속이 λΆˆκ°€λŠ₯ν•œ 경우

  • λΆ€λͺ¨ 클래슀의 private μ ‘κ·Όμ œν•œμ„ κ°–λŠ” ν•„λ“œ 및 λ©”μ†Œλ“œλŠ” μžμ‹μ΄ 물렀받을 수 μ—†μŒ.
  • λΆ€λͺ¨μ™€ μžμ‹ ν΄λž˜μŠ€κ°€ μ„œλ‘œ λ‹€λ₯Έ νŒ¨ν‚€μ§€μ— μžˆλŠ” 경우, λΆ€λͺ¨μ˜ default μ ‘κ·Ό μ œν•œμ„ κ°–λŠ” ν•„λ“œ 및 λ©”μ†Œλ“œλŠ” μžμ‹μ΄ 물렀받을 수 μ—†μŒ.(default μ ‘κ·Ό μ œν•œμ€ '같은 νŽ˜μ΄μ§€μ— μžˆλŠ” 클래슀'만 접근이 κ°€λŠ₯ν•œ μ ‘κ·Όμ œν•œμžμ΄κΈ° λ•Œλ¬Έ)

πŸ“Œ 상속이 ν•„μš”ν•œ 이유

  • 이미 λ§ˆλ ¨λ˜μ–΄μžˆλŠ” 클래슀λ₯Ό μž¬μ‚¬μš©ν•΄μ„œ λ§Œλ“€ 수 있기 λ•Œλ¬Έμ— 효율적이고 개발 μ‹œκ°„μ΄ 단좕됨.
  • 예제
public class ParentBook{
  String name; //ν•„λ“œ
  int price;
public void Print(){ // λ©”μ†Œλ“œ
  System.out.println("μ±…μ˜ 이름과 가격 : "+name+" "+price);
}
public class ChildBook extends ParentBook{
  ChildBook (String name, int price){ // μƒμ„±μž
    this.name = name;  //  ChildBook이 ParetBook의 ν•„λ“œλ₯Ό μƒμ†λ°›μ•„μ„œ κ°€λŠ₯ν•œ μ„ μ–Έ
    this.price = price; //  "
}
public static void main (String[] args){
  ChildBook Child = new ChildBook("λ‚˜μ˜ λΌμž„μ˜€λ Œμ§€ λ‚˜λ¬΄", 10000);
  System.out.print("[κ΅¬ν˜„ κ²°κ³Ό 1] ");
  Child.Print();
}
  • [κ΅¬ν˜„ κ²°κ³Ό] μ±…μ˜ 이름과 가격: λ‚˜μ˜ λΌμž„μ˜€λ Œμ§€λ‚˜λ¬΄ 10000
  • ChildBook ν΄λž˜μŠ€κ°€ ParentBook의 ν•„λ“œμ™€ λ©”μ†Œλ“œλ₯Ό μƒμ†λ°›μŒ. ChildBook 클래슀 내에 λ”°λ‘œ ν•„λ“œλ‚˜ λ©”μ†Œλ“œκ°€ μ„ μ–Έλ˜μ–΄ μžˆμ§€ μ•Šμ•˜λŠ”λ°λ„, μƒμ„±μžμ˜ this.name μ„ μ–Έμ΄λ‚˜, main λ©”μ†Œλ“œμ˜ Child.Print() κ°€ 컴파일 μ—λŸ¬κ°€ λ‚˜μ§€ μ•Šκ²Œ 됨.
  • μ°Έκ³  λΈ”λ‘œκ·Έ : https://chanhuiseok.github.io/posts/java-1/

πŸ“Œ 상속(Inheritance) 예제

예제 1.

public class BusinessMan extends Man{
    String company;
    public BusinessMan(){
//        super(10); //λΆ€λͺ¨ν΄λž˜μŠ€μ˜ μƒμ„±μžλ₯Ό μ‹€ν–‰ν•˜λŠ” 문법
//        System.out.println("BusineeMan 클래슀의 μƒμ„±μž μ‹€ν–‰~");
        super("홍길동");
        company = "λ―Έμ§€μ •";
    }
}
public class Man {
    String name;
    public Man(String name){
        this.name = name;
        System.out.println("Man 클래슀의 μƒμ„±μž μ‹€ν–‰~");
    }
}
public class Test1 {
    public static void main(String[] args) {
        BusinessMan m1 = new BusinessMan();
    }
}

예제 2.

  • λ‹€μŒ 클래슀λ₯Ό 기반으둜 main() λ©”μ†Œλ“œκ°€ 싀행될 수 μžˆλ„λ‘ ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•΄λ³΄μ‹œμ˜€.
public class A {
    private int x;
    private int y;
    public A(){
        x = 1;
        y = 1;
    }
    public A(int x){
        this.x = x;
        y = 1;
    }
    public A(int x, int y){
        this.x = x;
        this.y = y;
    }
    public void disp(){
        System.out.print(" x = " + x + ", y = " + y);
    }
}
class B extends A{
    private int x;
    private int y;
    public B(){
        x = 1;
        y = 1;
    }
    public B(int x){
        super(x);
        this.x = 1;
        y = 1;
    }
    public B(int x, int y){
        super(x, y);
        this.x = 1;
        this.y = 1;
    }
    public void disp(){
        super.disp();
        System.out.println(" x = " + x + ", y = " + y);
    }
}
public class Work {
    public static void main(String[] args) {
            B bp = new B();
            B bp1 = new B(10);
            B bp2 = new B(10, 20);
            bp.disp(); // x = 1, y = 1, x = 1, y = 1
            bp1.disp(); // x = 10, y = 1, x = 1, y = 1
            bp2.disp(); //// 10 = 1, y = 20, x = 1, y = 1
    }
}

πŸ€ 회고

  • λΆ€λͺ¨-μžμ‹ κ΄€κ³„μ—μ„œ 클래슀 λ‚΄μ˜ λ³€μˆ˜μ™€ λ©”μ†Œλ“œλ₯Ό 상속받을 수 μžˆλŠ” '상속(Inheritance)'에 λŒ€ν•΄ λ°°μ› λ‹€. μ΄λŸ¬ν•œ κΈ°λŠ₯ 덕뢄에 훨씬 효율적이고 경제적인 ν”„λ‘œκ·Έλž˜λ° 개발이 κ°€λŠ₯ν•˜λ‹€λŠ” 것을 μ•Œμ•˜λ‹€. 원둠적인 κ°œλ…μ€ μ–΄λŠμ •λ„ μ΄ν•΄ν–ˆλ‹€κ³  μƒκ°ν–ˆλŠ”λ°, μ—­μ‹œ 예제λ₯Ό 톡해 λ‚΄κ°€ 직접 μž‘μ„±ν•˜μ—¬ μ μš©ν•˜λŠ” κ²ƒκ³ΌλŠ” 큰 괴리가 μžˆλ‹€. μ†μœΌλ‘œ 더 많이 μž‘μ„±ν•˜λ©΄μ„œ μ—°μŠ΅ν•΄μ•Όκ² λ‹€...
profile
λ¨Ήκ³  κΈ°λ„ν•˜κ³  μ½”λ”©ν•˜λΌ

0개의 λŒ“κΈ€