본문 바로가기

언어/Java

상속 예제문제

public class ChildExample {
	public static void main(String[] args) {
		Child child = new Child();
	}
}​
public class Parent {
	public String nation;
	
	public Parent() {
		this("대한민국");	// 얘를 불러줄 아래 메서드가 필요함
		System.out.println("Parent() call");
	}
	
	// 오버로드 되어있음
	public Parent(String nation) {
		// super(); 이지만 object
		this.nation = nation;
		System.out.println("Parent(String nation) call");
	}
}
public class Child extends Parent {
	public String name;
	
	// 1번 호출
	public Child() {
		this("홍길동");	// 이걸 처리하기 위해 밑의 메서드로
		System.out.println("Child() call");
	}
	public Child(String name) {
		// super();가 생략되어 있음 Parent로 넘어감
		this.name = name;
		System.out.println("Child(String name) call");
	}
}

실행 결과

'언어 > Java' 카테고리의 다른 글

컬렉션 프레임워크(Java Collection Framework)  (0) 2023.08.04
어노테이션(Annotation)  (0) 2023.08.04
가변길이 매개변수  (0) 2023.08.04
break, return, continue  (0) 2023.08.04
NaN과 Infinity  (0) 2023.08.04