Super

ExiNni·2023년 9월 11일
0

JAVA

목록 보기
15/20

super. 과 super()의 차이

Java 에서 생성자가 호출될 때, 첫 번쨰 작업으로 super 자동으로 호출됨
생성자 체인: 클래스 계층 구조에서 상속을 통해 연결된 여러 생성자들 간의 호출 순서를 관리하는 것

1. super. : 부모 클래스의 변수나 메서드에 접근하기 위해 사용
2. super(): 부모 클래스의 생성자를 호출하기 위해 사용
			서브 클래스으 생성자에서 첫 번째 줄에 호출되어야 함
ex) 
	class Parent(){
		String name;
		Parent(){
			// 기본 생성자
		}
		Parent(String name){
			this.name = name;
			// 자식 문자열 파라미터 생성자
		}
		void show(){
			System.out.println("출력 메서드");
		}
	}
	
	class Child extends Parent{
		Child(){
			super(); // 부모 클래스의 기본 생성자 호출
			System.out.println("자식 클래스의 기본 생성자");
		}
		void display(){
			super.show(); // 부모 클래스의 show 메서드 호출
		}
	}
	
	Public Main(){
		public static void main(String[] args){
			Child ch1 = new Child();
			Child ch2 = new Child();
			
			ch2.display();
		}
	}
profile
Software Developer

0개의 댓글