Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- super
- 클래스
- 반복문
- 멤버변수
- 변수
- do while문
- Interface
- After effects
- if문
- 생성자
- Photoshop
- static
- private
- 파라미터
- 형변환
- length
- 배열
- while문
- 메서드
- Java
- symbol
- Override
- illustrator
- 상속
- expand
- for문
- Singleton
- Overload
- 조건문
- switch문
Archives
- Today
- Total
Ms'Note
Extends-1 본문
부모 클래스의 작성
- 덧셈과 뺄셈의 기능을 갖는 클래스를 준비한다.
public class CalcParent {
public int plus(int x, int y) {
return x + y;
}
public int minus(int x, int y) {
return x - y;
}
}
- 생성자를 특별히 정의하지 않는 경우 자바 컴파일러가 기본 생성자가 있다고 간주하기 때문에,
클래스 다이어그램에서는 기본 생성자를 명시한다.
자식 클래스의 작성 (클래스 다이어그램 구현)
- 부모의 모든 기능을 상속받고 있으며, 곱셈과 나눗셈을 추가하여 부모의 기능을 확장시켰다.
// CalcParent를 상속받는 케이스
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) {
result = x / y;
}
return result;
}
}
CalcParent와 CalcChild의 활용
- CalcParent를 상속받는 CalcChild 클래스의 객체는 부모에게서 상속받은
plus() 메서드와 minus() 메서드를 사용할 수 있다.
public class Main01 {
public static void main(String[] args) {
CalcParent parent = new CalcParent();
System.out.println(parent.plus(100, 50));
System.out.println(parent.minus(100, 50));
System.out.println("---------------------");
CalcChild child = new CalcChild();
System.out.println(child.plus(200, 100));
System.out.println(child.minus(200, 100));
System.out.println(child.times(200, 100));
System.out.println(child.divide(200, 100));
}
}
출력 결과
150
50
---------------------
300
100
20000
2
Comments