2021.01.29 국비교육 19일차

[TOC]

IS A 관계

  • 같은 종의 비슷한 속성 및 동작을 가진 객체들 간의 관계
package com.test;

class Employee{

    String name;
    int salary;

    public Employee() {}

    public Employee(String name, int salary) {
        this.name = name;
        this.salary = salary;
    }    
}

class Manager extends Employee{
    String depart;

    public String getManager() {

        return name + "\t" + salary + "\t" + depart;
    }

    //public Manager() {}

    public Manager(String name, int salary, String depart) {
        super(); // 부모의 기본생성자 호출
        this.name = name;
        this.salary = salary;
        this.depart = depart;
    }
}


public class Ex06_1 {

    public static void main(String[] args) {
        Employee emp = new Employee("홍길동", 2000);
        Manager man = new Manager("이순신", 400, "개발");

    }
}

상속[inheritance]

  • 반드시 객체간에 IS A 관계가 성립되어야 한다.

  • 생성자와 private로 지정된 멤버는 상속이 안된다.

  • extends 키워드를 사용하여 상속관계 표시

  • 생성자는 상속되지 않음

  • 모든 클래스는 부모객체를 먼저 생성한 후 자신이 생성

  • 부모생성자를 호출하기 위해서 super() 사용

package com.test3;
class Employee{

    String name;
    int salary;

    public String getEmployee(){
        System.out.println("employee getEmployee 출력");
        return name +  "\t" + salary;
    }

    public Employee() {}

    public Employee(String name, int salary) {
        this.name = name;
        this.salary = salary;
    }



}


class Engineer extends Employee{
    int bonus;

    public Engineer(String name, int salary, int bonus) {
        super(name, salary);
        this.bonus = bonus;
    }

    @Override 
    public String getEmployee(){
        System.out.println("Engineer getEmployee 출력");
        return super.getEmployee() + "\t" + bonus;
    }
}


class Manager extends Employee{

    String depart;

    @Override
    public String getEmployee() {
        System.out.println("manager getemployee 출력");
        return super.getEmployee() + "\t" + depart;
    }

    public Manager() {}

    public Manager(String name, int salary, String depart) {
        super(name, salary);
        this.depart = depart;
    }


}

public class Ex06_6 {

    public static void main(String[] args) {


    }

}

+ Recent posts