# [Java] 상속(Inheritance)에 λŒ€ν•΄
Study Repository

[Java] 상속(Inheritance)에 λŒ€ν•΄

by rlaehddnd0422

μžλ°”μ—λŠ” μƒμ†μ΄λž€ κ°œλ…μ΄ μ‘΄μž¬ν•©λ‹ˆλ‹€.

 

μƒμ†μ΄λž€?

상속은 μ‰½κ²Œ 말해 λΆ€λͺ¨ ν΄λž˜μŠ€μ™€ μžμ‹ ν΄λž˜μŠ€κ°€ μžˆμ„ λ•Œ, μžμ‹ ν΄λž˜μŠ€λŠ” λΆ€λͺ¨ 클래슀λ₯Ό μ„ νƒν•΄μ„œ κ·Έ λΆ€λͺ¨μ˜ 멀버λ₯Ό λ¬Όλ €λ°›μ•„ κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜λŠ” 것을 λ§ν•©λ‹ˆλ‹€.

 

μžμ‹ 클래슀 = λΆ€λͺ¨ ν΄λž˜μŠ€λ‘œλΆ€ν„° 상속받은 멀버 + μΆ”κ°€λœ 멀버(μ˜΅μ…˜)

β€» μ—¬κΈ°μ„œ λ§ν•˜λŠ” λ©€λ²„λŠ” μΈμŠ€ν„΄μŠ€ λ³€μˆ˜μ™€, μΈμŠ€ν„΄μŠ€ λ©”μ†Œλ“œλ₯Ό λ§ν•©λ‹ˆλ‹€.

 

λ‹€λ§Œ, μ£Όμ˜ν•΄μ•Ό ν•  점.

1. μ ‘κ·Ό μ œμ–΄μžκ°€ private인 κ²½μš°μ—λŠ” μžμ‹ ν΄λž˜μŠ€μ—μ„œ 상속이 λΆˆκ°€λŠ₯ ν•©λ‹ˆλ‹€.

public class Parent {
    private int a = 1;
    public int b = 2;

    private void printA() {
        System.out.println(a);
    }

    public void printB() {
        System.out.println(b);
    }
}
  • Parent ν΄λž˜μŠ€λŠ” a와 b의 μΈμŠ€ν„΄μŠ€ λ³€μˆ˜λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. 이 λ•Œ aλŠ” μ ‘κ·Όμ œμ–΄μžκ°€ private이기 λ•Œλ¬Έμ—, μΆ”ν›„ ν•˜μœ„ ν΄λž˜μŠ€μ—μ„œ 이 클래슀λ₯Ό 상속받더라도 a에 λŒ€ν•΄ μ ‘κ·Όν•  μˆ˜λŠ” μ—†μŠ΅λ‹ˆλ‹€.
  • λ§ˆμ°¬κ°€μ§€λ‘œ ν•˜μœ„ ν΄λž˜μŠ€μ—μ„œ printA()와 printB()의 μΈμŠ€ν„΄μŠ€ λ©”μ†Œλ“œλ₯Ό κ°–λŠ” Parent ν΄λž˜μŠ€μ— λŒ€ν•΄ 상속받을 λ•Œ, printB()에 λŒ€ν•œ 상속은 κ°€λŠ₯ν•˜μ§€λ§Œ, μ ‘κ·Όμ œμ–΄μžκ°€ private인 printA() μΈμŠ€ν„΄μŠ€ λ©”μ†Œλ“œμ— λŒ€ν•œ 상속은 이루어지지 μ•ŠμŠ΅λ‹ˆλ‹€.  
public class Child extends Parent{
    public int c = 3;
    public void printC() {
        System.out.println("c = " + c);
    }
}
public class ChildTest {
    public static void main(String[] args) {
        Child c = new Child();
        c.printA(); // ERROR, λΆ€λͺ¨ 클래슀의 printA()λŠ” privateμ΄λ―€λ‘œ μƒμ†λ˜μ§€ μ•ŠμŒ
        c.printB();
        c.printC();
    }
}

 

2. λΆ€λͺ¨μ™€ μžμ‹ ν΄λž˜μŠ€κ°€ μ„œλ‘œ λ‹€λ₯Έ νŒ¨ν‚€μ§€μ— μžˆλŠ” κ²½μš°μ—λŠ” λΆ€λͺ¨ 클래슀의 멀버가 default μ ‘κ·Ό μ œμ–΄μžλ₯Ό κ°–λŠ” κ²½μš°μ—λ„ 상속이 λΆˆκ°€λŠ₯

λ°˜λŒ€λ‘œ λ§ν•˜λ©΄ 상/ν•˜μœ„ ν΄λž˜μŠ€κ°€ μ„œλ‘œ 같은 νŒ¨ν‚€μ§€μ— μžˆλŠ” 경우, default μ ‘κ·Ό μ œμ–΄μžλ₯Ό κ°–λŠ” 멀버에 λŒ€ν•œ 상속은 이루어진닀.

public class ParentBook {
    String title;
    String genre;

    public void print() {
        System.out.println("TITLE : " + title);
        System.out.println("GENRE : " + genre);
    }
}

ParentBook ν΄λž˜μŠ€λŠ” titleκ³Ό genreλΌλŠ” default μ ‘κ·Ό μ œμ–΄μžμ˜ μΈμŠ€ν„΄μŠ€ λ³€μˆ˜λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. 그리고 μΈμŠ€ν„΄μŠ€ λ³€μˆ˜λ₯Ό 좜λ ₯ν•˜λŠ” 상속 κ°€λŠ₯ν•œ public의 μΈμŠ€ν„΄μŠ€ λ©”μ†Œλ“œ print()λ₯Ό κ°–μŠ΅λ‹ˆλ‹€.

  • μ ‘κ·Όμ œμ–΄μžλŠ” μƒλž΅κ°€λŠ₯ν•˜λ©° μƒλž΅ν–ˆμ„ λ•ŒλŠ” μžλ™μœΌλ‘œ default μž„μ„ λœ»ν•˜κ²Œ λ©λ‹ˆλ‹€. λ”°λΌμ„œ default μΌκ²½μš°μ—λŠ” μ ‘κ·Όμ œμ–΄μžλ₯Ό μ§€μ •ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
public class ChildBook extends ParentBook{

    ChildBook(String title, String genre) {
        this.title = title;
        this.genre = genre;
    }
}

ChildBookμ—μ„œλŠ” ParentBook을 μƒμ†λ°›μ•˜μŠ΅λ‹ˆλ‹€.

μƒμ†λœ λ©€λ²„λ‘œλŠ” μΈμŠ€ν„΄μŠ€ λ³€μˆ˜μ—λŠ” title, genre, μΈμŠ€ν„΄μŠ€ λ©”μ†Œλ“œμ—λŠ” print()λ₯Ό 상속 λ°›μ•˜μŠ΅λ‹ˆλ‹€. λΆ€λͺ¨ ν΄λž˜μŠ€λ‘œλΆ€ν„° titleκ³Ό genre에 λŒ€ν•œ 멀버 λ³€μˆ˜λ₯Ό μƒμ†λ°›μ•˜κΈ° λ•Œλ¬Έμ—, μƒμ„±μžμ—μ„œ this.titleκ³Ό this.genre에 λŒ€ν•œ 컴파일 μ—λŸ¬κ°€ λ‚˜μ§€ μ•ŠλŠ” κ²ƒμž…λ‹ˆλ‹€.

public class ChildBookTest {
    public static void main(String[] args) {
        ChildBook c = new ChildBook("노인과 λ°”λ‹€", "μ†Œμ„€");
        c.print();
    }
}

ChildBook μΈμŠ€ν„΄μŠ€λ₯Ό ν•˜λ‚˜ λ§Œλ“€μ–΄ print() λ©”μ†Œλ“œλ₯Ό 싀행해보면 λ‹€μŒκ³Ό 같은 좜λ ₯κ²°κ³Όλ₯Ό 얻을 수 μžˆμŠ΅λ‹ˆλ‹€.

c.print()κ°€ κ°€λŠ₯ν•œ μ΄μœ λŠ” λΆ€λͺ¨ ν΄λž˜μŠ€λ‘œλΆ€ν„° print() λ©”μ†Œλ“œλ₯Ό μƒμ†λ°›μ•˜κΈ° λ•Œλ¬Έμ— κ·ΈλŒ€λ‘œ μ‚¬μš©μ΄ κ°€λŠ₯ν•œλ°, λ‹¨μˆœν•˜κ²Œ μžμ‹ ν΄λž˜μŠ€μ—μ„œλŠ” 상속받은 λΆ€λͺ¨μ˜ 멀버λ₯Ό κ·ΈλŒ€λ‘œ λ¬Όλ €λ°›μ•„ μ‚¬μš©ν•  수 μžˆλ‹€κ³  μƒκ°ν•˜μ‹œλ©΄ λ©λ‹ˆλ‹€.

 

이 λ•Œ λΆ€λͺ¨μ˜ 멀버λ₯Ό κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜μ§€ μ•Šκ³  μžμ‹ ν΄λž˜μŠ€μ—μ„œ ν•΄λ‹Ή λ©”μ†Œλ“œλ₯Ό μž¬μ •μ˜ν•˜μ—¬ μ‚¬μš©ν•˜κ³  싢은 경우, λ‹€μŒκ³Ό 같이 λΆ€λͺ¨ν΄λž˜μŠ€μ˜ λ©”μ†Œλ“œλ₯Ό μ˜€λ²„λΌμ΄λ”© ν•˜μ—¬ μžμ‹ ν΄λž˜μŠ€μ—μ„œ κΈ°λŠ₯을 μž¬μ •μ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

public class ChildBook extends ParentBook{

    public String writer;

    ChildBook(String title, String genre, String writer) {
        this.title = title;
        this.genre = genre;
        this.writer = writer;
    }

    @Override
    public void print() {
        System.out.println("TITLE IS " + title + " AND GENRE IS" + genre);
        System.out.println("Written By " + writer);
    }
}
public class ChildBookTest {
    public static void main(String[] args) {
        ChildBook c = new ChildBook("노인과 λ°”λ‹€", "μ†Œμ„€", "해밍웨이");
        c.print();
    }
}

 

λΆ€λͺ¨ 클래슀 호좜 : super(λ§€κ°œλ³€μˆ˜ 리슀트);

사싀 μžλ°”μ—μ„œλŠ” μžμ‹ μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜λ©΄, λΆ€λͺ¨ μΈμŠ€ν„΄μŠ€λ₯Ό λ¨Όμ € μƒμ„±ν•œ ν›„, μžμ‹ μΈμŠ€ν„΄μŠ€κ°€ κ·Έ λ‹€μŒμ— μƒμ„±λ©λ‹ˆλ‹€.

이 λ•Œ λΆ€λͺ¨ 클래슀의 λͺ…μ‹œμ μΈ μƒμ„±μžκ°€ μ—†λŠ” 경우, μ»΄νŒŒμΌλŸ¬λŠ” μ•Œμ•„μ„œ κΈ°λ³Έ μƒμ„±μž(super();)λ₯Ό ν˜ΈμΆœν•©λ‹ˆλ‹€. 

public class ParentBook {
    String title;
    String genre;
    
//    ParentBook(){} μžλ™μœΌλ‘œ 호좜

    public void print() {
        System.out.println("TITLE : " + title);
        System.out.println("GENRE : " + genre);
    }
}
public class ChildBook extends ParentBook{

    public String writer;

    ChildBook(String title, String genre, String writer) {
    	super();
        this.title = title;
        this.genre = genre;
        this.writer = writer;
    }

    @Override
    public void print() {
        System.out.println("TITLE IS " + title + " AND GENRE IS" + genre);
        System.out.println("Written By " + writer);
    }

 

λΆ€λͺ¨ ν΄λž˜μŠ€λŠ” λͺ…μ‹œμ μΈ μƒμ„±μžκ°€ μ—†κ³ , μžμ‹ ν΄λž˜μŠ€μ—” λͺ…μ‹œμ μΈ μƒμ„±μžκ°€ μ‘΄μž¬ν•  λ•Œ μžμ‹ 클래슀의 μƒμ„±μž μ„ μ–Έ 내뢀에 맨 첫쀄에 super(); 라고 μƒμ„±λ©λ‹ˆλ‹€. (이와 같은 경우 μž‘μ„±ν•˜μ§€ μ•Šμ•„λ„ μ•Œμ•„μ„œ μƒμ„±ν•˜λ―€λ‘œ μƒλž΅ν•΄λ„ OK)

 

ν•˜μ§€λ§Œ λ§Œμ•½ λΆ€λͺ¨ ν΄λž˜μŠ€μ— λͺ…μ‹œμ μΈ μƒμ„±μžκ°€ μ‘΄μž¬ν•˜λŠ” 경우, λ°˜λ“œμ‹œ μžμ‹ ν΄λž˜μŠ€μ—μ„œλŠ” super(λ§€κ°œλ³€μˆ˜ 1, λ§€κ°œλ³€μˆ˜ 2 ...);의 ν˜ΈμΆœμ„ 톡해 λΆ€λͺ¨ 클래슀의 λͺ…μ‹œμ  μƒμ„±μžλ₯Ό 직접 ν˜ΈμΆœν•΄μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€.

public class ParentBook {
    String title;
    String genre;

    ParentBook(String title, String genre) {
        this.title = title;
        this.genre = genre;
    }

    public void print() {
        System.out.println("TITLE : " + title);
        System.out.println("GENRE : " + genre);
    }
}
  • λΆ€λͺ¨ ν΄λž˜μŠ€μ— λͺ…μ‹œμ  μƒμ„±μžκ°€ 쑴재
public class ChildBook extends ParentBook{

    private String writer;

    ChildBook(String title, String genre, String writer) {
        super(title, genre);
//        this.title = title;
//        this.genre = genre;
        this.writer = writer;
    }
}
  • μžμ‹ ν΄λž˜μŠ€μ—μ„œλŠ” λΆ€λͺ¨ 클래슀의 λͺ…μ‹œλœ μƒμ„±μžλ₯Ό super(λ§€κ°œλ³€μˆ˜ 리슀트)λ₯Ό λ°˜λ“œμ‹œ 첫 쀄에 직접 ν˜ΈμΆœν•΄ μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€. (그렇지 μ•ŠμœΌλ©΄ 컴파일 μ—λŸ¬ λ°œμƒ)
  • 이 λ•Œ λΆ€λͺ¨ 클래슀 μƒμ„±μžλ₯Ό ν˜ΈμΆœν•˜μ—¬ 멀버 λ³€μˆ˜κ°’μ΄ ν• λ‹Ήλ˜μ—ˆμœΌλ―€λ‘œ λ³„λ„λ‘œ this ν˜ΈμΆœν•˜μ—¬ 값을 또 할당해쀄 ν•„μš”κ°€ X

<참고 자료>

 

μžλ°”(JAVA) - 상속(Inheritance) (1) 클래슀 상속, λΆ€λͺ¨ μƒμ„±μžμ˜ 호좜

μƒμ†μ΄λž€?

chanhuiseok.github.io

 

 

 

 

λΈ”λ‘œκ·Έμ˜ 정보

Study Repository

rlaehddnd0422

ν™œλ™ν•˜κΈ°