Programing/JAVA

상속 INHERITANCE_Part1

쿠크 2022. 3. 3. 16:52
  • 클래스의 수직 구조 설계
  • extends 예약어 사용
  • 부모 클래스를 자식 클래스가 상속받으면 부모 클래스의 변수와 메서드가 상속됨
  • 부모 클래스의 변수와 메서드를 자신의 것처럼 사용 가능
  • 부모클래스는 슈퍼(super), 상위, 베이스 클래스 라고도 부름
  • 자식클래스는 서브, 하위, 파생 클래스 라고도 부름

부모클래스

package chapter08;

public class Phone {

    String name;
    String color;
    String company;

    void call() {
        System.out.println("전화를 건다");
    }

    void receive() {
        System.out.println("전화를 받다");
    }

}

자식클래스

package chapter08;

public class SmartPhone extends Phone {

    public void installApp() {
        System.out.println("앱 설치");
    }

}

클래스 사용

package chapter08;

public class SmartPhoneMain {

    public static void main(String[] args) {

        Phone p = new Phone();
        p.name = "전화기";
        p.company = "현대";
        p.color = "화이트";

        System.out.println("Phone 출력");
        System.out.println(p.name);
        System.out.println(p.company);
        System.out.println(p.color);
        p.call();
        p.receive();

        SmartPhone sp = new SmartPhone();
        sp.name = "갤럭시";
        sp.company = "삼성";
        sp.color = "블랙";

        System.out.println("SmartPhone 출력");
        System.out.println(sp.name);
        System.out.println(sp.company);
        System.out.println(sp.color);
        sp.call();
        sp.receive();
        sp.installApp();

    }
}

super

  • 부모 인스턴스 : 자식 클래스에서 super.변수 접근 가능
  • super() : 상속 받은 부모의 인스턴스 생성자
  • 상속 받은 자식클래스 인스턴스 생성시 부모 클래스가 먼저 만들어 저야 함
  • 부모와 자식 클래스 모두 기본생성자 만 있으면 모두 생락 가능
  • 부모의 클래스에 오버로딩된 생성자가 있다면 자식 클래스에서 super()로 먼저 호출 해 주어야 함

부모 클래스 기본 생성자

부모클래스

package chapter08;

public class Phone {

    // 기본 생성자 생략    
    public Phone() {
    }

}

자식클래스

package chapter08;

public class SmartPhone extends Phone {

    // 기본생성자 생략가능
    public SmartPhone () {
        // super() -> 부모기본생성자 생략가능
        super() {
        }
    }
}

클래스 사용

package chapter08;

public class SmartPhoneMain {

    public static void main(String[] args) {

                // SmartPhone() -> super() --> 부모인스턴스 생성 --> 자식인스턴스 생성
        SmartPhone sp = new SmartPhone();       
    }
}

부모 클래스 오버로딩 생성자만 있을 때

부모클래스

package chapter08;

public class Phone {

    String name;
    String color;
    String company;

       //  오버로딩 생성자
    public Phone(String name, String color, String company) {
        this.name = name;
        this.color = color;
        this.company = company;
    }
}

자식클래스

  • 부모클래스 생성자 super() 생략 불가
package chapter08;

public class SmartPhone extends Phone {


    public SmartPhone() {
               // 부모 클래스 생성자 호출 
        super("갤럭시", "블랙", "삼성");
    }
}

클래스 사용

package chapter08;

public class SmartPhoneMain {

    public static void main(String[] args) {

        // SmartPhone() -> super() --> 부모인스턴스 생성 --> 자식인스턴스 생성
        SmartPhone p = new SmartPhone();        
    }
}

메서드 재정의 overriding

  • 상속관계에서 부모 클래스의 메서드를 자식 클래스에서 다시 정의 하는 것
  • 자식클래스 인스턴스에서는 재정의된 메서드 호출됨
  • 다형성에서도 참조된 참조변수에서도 재정의된 자식 메서드 호출됨

부모클래스

package chapter08;

public class Car {

    String color;
    String name;

    public void go() {
        System.out.println("전진");
    }

    void back() {
        System.out.println("후진");
    }
}

자식클래스

package chapter08;

public class Taxi extends Car {

    public void go() {
        System.out.println("미터기를 켜고 전진");
    }
}

클래스 실행

package chapter08;

public class TaxiMain {

    public static void main(String[] args) {

        Taxi t = new Taxi();
        t.go(); 

    }
}