JAVA 프로그래밍

[JAVA] 풀스택 개발자 부트캠프 011일차 ② protected, over ride, super

selfdailycoding 2025. 1. 10. 09:15

protected
(외부로부터)보호하다
자식클래스에서는 접근이 가능하지만 외부에서 접근은 안됨

Over Ride
상속 받은 클래스에서 고쳐(추가, 보강) 기입

super
부모 클래스를 가리키는 주소(pointer)


main.MainClass

package main;

import cls.Child;
import cls.Parent;

public class MainClass {

    public static void main(String[] args) {

        Parent parent = new Child();
        parent.method();
        parent.setName("");
    }
}

cls.Parent

package cls;

public class Parent {

    protected String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void method(){
        System.out.println("Parent method()");
    }
}

cls.Child

package cls;

public class Child extends Parent{
    private int number;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public void func(){
        //name = "홍길동";
        setName("홍길동");
        System.out.println("Child func()");
    }

    public void method(){ // over ride
       System.out.println("Child method()");
    }
}