java_inheritance(상속)

AMJ·2023년 6월 13일
0

code_log_java

목록 보기
25/29

참고사이트 : https://www.youtube.com/watch?v=HPJFi6W7oco&list=PLlTylS8uB2fA5PevGI6ARN6v-VQWmqaix&index=6


상속 : 부모(super class) >> 자식( sub class)

UML : Unified Modeling Language
UML 참고 : https://m.blog.naver.com/icbanq/221781238065

public class Person{
}
public class Worker extends Person{
}
public class Student extends Person{
}
public class Doctor extends Worker{
}


Ex.

// package com.ll.PT.inheritance;

public class Parent {
public Parent() {
System.out.println("Parent noArgConstructor");
}
public void printName(){
System.out.println("Parent method");
}
}

```java
// package com.ll.PT.inheritance;

public class Child extends Parent {
    public Child() {
        System.out.println("Child noArgConstructor");
    }

    @Override
    public void printName() {
//        super.printName(); // 부모의 기능 그대로 사용시
        System.out.println("Child Override printName"); // override 적용됨
    }
}
package com.ll.PT.inheritance;

public class inheritPrint {
    public static void main(String[] args) {
        Child child = new Child();  // 생성자 호출됨
        child.printName(); // method 실행됨
    }
}
profile
재미있는 것들

0개의 댓글