수업소개
생성자가 있는 클래스를 상속할 때 상위 클래스의 생성자를 실행해야 합니다. 그 방법에 대해서 알아봅니다.
강의
소스코드
https://github.com/egoing/java-inheritance/commit/179c452c4b2a782f97fc395d7366af426d782e1e
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Cal{ int v1,v2; Cal( int v1, int v2){ System.out.println( "Cal init!!" ); this .v1 = v1; this .v2 = v2; } public int sum(){ return this .v1+v2;} } class Cal3 extends Cal{ Cal3( int v1, int v2) { super (v1, v2); System.out.println( "Cal3 init!!" ); } public int minus(){ return this .v1-v2;} } public class InheritanceApp { public static void main(String[] args) { Cal c = new Cal( 2 , 1 ); Cal3 c3 = new Cal3( 2 , 1 ); System.out.println(c3.sum()); // 3 System.out.println(c3.minus()); // 1 } } |