extends 키워드를 사용하여 부모 클래스의 이름을 명시한다.
public class 자식클래스 extends 부모클래스 {...}
package Extends;
public class CalcParent {
public int plus(int x, int y) {
return x+y;
}
public int minus(int x, int y) {
return x-y;
}
}
package Extends;
public class CalcChild extends CalcParent {
public int times(int x, int y) {
return x * y;
}
public int divide(int x, int y) {
int result = 0;
if(y!=0) {
return x / y;
}
return result;
}
}
package Extends;
public class Main01 {
public static void main(String[] args) {
CalcParent parent = new CalcParent();
System.out.println(parent.plus(20, 10));
System.out.println(parent.minus(20, 10));
System.out.println("-================================");
CalcChild child = new CalcChild();
System.out.println( child.times(20, 10));
System.out.println( child.divide(20, 10));
System.out.println( child.plus(20, 10));
System.out.println( child.minus(20, 10));
}
}