YS's develop story
JAVA error) Implicit super constructor is undefined, Constructor call must be the first statement in a constructor. 본문
			Java
			
		JAVA error) Implicit super constructor is undefined, Constructor call must be the first statement in a constructor.
Yusang 2020. 10. 14. 08:07안녕하세요 ~

오늘은 java 상속과 관련해서 에러를 정리하는 글을 작성했습니다 :)
아래와 같이 코드를 작성하게 되면 에러가 발생하게 됩니다.
package javapractice;
class JavaPractice {
	public static void main(String[] args) {
		
		subClass sub1= new subClass(52);
		System.out.println(sub1.i);
		}
	}
class superClass{
	
	int i;
	superClass(int i){
		System.out.println("this is super class");
		this.i=i;
	}	
}
class subClass extends superClass{
	subClass(int i){
		System.out.println("this is sub class");
	}
}
	

java에서는 상속을 할 경우 상위클래스의 생성자가 값을 인자로 받는다면
super 키워드를 통해 상위클래스의 생성자를 초기화 해주어야 합니다.
그렇기 않으면 위와 같이 에러가 발생합니다.
아래와 같이 코드를 수정 하면...
package javapractice;
class JavaPractice {
	public static void main(String[] args) {
		
		subClass sub1= new subClass(52);
		System.out.println(sub1.i);
		}
	}
class superClass{
	
	int i;
	superClass(int i){
		System.out.println("this is super class");
		this.i=i;
	}	
}
class subClass extends superClass{
	subClass(int i){
		System.out.println("this is sub class");
		super(i);
	}
}
	
어라..? 또 에라가 발생합니다.

Super를 이용한 상위 클래스의 생성자 호출문은 무조건 생성자의 첫 문장에 와야합니다.
그렇지 않으면 위와 같이 에러가 발생합니다.
아래와 같이 코드를 수정하면 문제 없이 실행이 됩니다.
package javapractice;
class JavaPractice {
	public static void main(String[] args) {
		
		subClass sub1= new subClass(52);
		System.out.println(sub1.i);
		}
	}
class superClass{
	
	int i;
	superClass(int i){
		System.out.println("this is super class");
		this.i=i;
	}	
}
class subClass extends superClass{
	subClass(int i){
		super(i);
		System.out.println("this is sub class");
	}
}
	
super를 이용해 상위 클래스의 생성자를 호출 할 때
코드를 첫 문장에 작성하지 않아 에러가 발생하는 실수를 하지 맙시다 ~

마무리는 쌈무 짤로..
'Java' 카테고리의 다른 글
| JAVA error) The method must override or implement a supertype method (1) | 2020.11.06 | 
|---|---|
| Java ) java 상속관련 .. (0) | 2020.10.16 | 
| JAVA ) java 2차원 배열 관련.. , 열혈 java 프로그래밍 309쪽 문제 2번 (0) | 2020.10.10 | 
| JAVA ) static이라고 선언된 변수, 클래스 변수 (0) | 2020.10.04 | 
| JAVA ) 클래스의 String toString() 메소드 (0) | 2020.09.29 | 
			  Comments
			
		
	
               
           
					
					
					
					
					
					
				 
								 
								 
								