8 public class Sum Test public static void
8장. 상속 public class Sum. Test { public static void main(String a 1[]) { int a, b, sum; a = Integer. parse. Int(a 1[0]); b = Integer. parse. Int(a 1[1]); sum = a + b ; // 두 수를 더하는 부분입니다 System. out. println("두수의 "); System. out. println("두수의 합은 " + sum + "입니다"); } } 1
§ 목차 l section 1 상속(Inheritance)의 개요 l section 2 멤버 변수의 상속 l section 3 메소드의 상속과 오버라이딩(Overriding) l section 4 예약어 super l section 5 속과 생성자 l section 6 객체의 형변환 l section 7 연산자 instanceof l section 8 추상 클래스와 추상 메소드 l section 9 예약어 final 3
2 멤버 변수의 상속 l 실습예제 Inheritance. Test 1. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 8장. 상속 class A { int aa = 1; } class B extends A { int bb = 2; } class C extends B { int cc = 3; } 세 개의 클래스가 상속 관계 public class Inheritance. Test 1 { public static void main(String[] args) { C objc = new C(); System. out. println ("objc객체의 객체속성변수 aa의 값은 " + objc. aa); System. out. println ("objc객체의 객체속성변수 bb의 값은 " + objc. bb); System. out. println ("objc객체의 객체속성변수 cc의 값은 " + objc. cc); } } 상속된 객체 변수에 접근 7
2 멤버 변수의 상속 l 실습예제 Inheritance. Test 2. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 8장. 상속 class AA { int i; private int j; 객체 변수 j를 private로 선언 void setij(int x, int y) { i = x; j = y; } } AA 클래스로부터 상속받아 BB 클래스 생성 class BB extends AA { int total; void sum() { total = i + j; 오류 발생 } } public class Inheritance. Test 2 { public static void main(String args[]) { BB sub. Ob = new BB(); sub. Ob. sum(); } } 9
2 멤버 변수의 상속 l 실습예제 Inheritance. Test 3. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 8장. 상속 class B 1 { int x; 객체 변수 x 선언 } class B 2 extends B 1 { String x; 객체 변수 x 선언. 상위 클래스의 객체 변수 x가 가려짐 } public class Inheritance. Test 3 { public static void main(String args[]) { B 2 b 2 = new B 2(); b 2. x = "알기 쉽게 해설한 자바"; x는 하위 클래스에서 선언된 문자열 System. out. println("객체 b 2에 들어있는 x 값 : " + b 2. x); B 1 b 1 = new B 1(); b 1. x = 5000; x는 상위 클래스에서 선언된 정수 System. out. println("객체 b 1에 들어있는 x 값 : " + b 1. x); } } 11
2 멤버 변수의 상속 l 실습예제 Inheritance. Test 4. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 8장. 상속 class C 1 { static int x; 두 개의 클래스 변수 선언 static int y; } class C 2 extends C 1 { static String x; 상속된 클래스에서 같은 이름을 가진 클래스 변수 선언 } class Inheritance. Test 4 { public static void main(String args[]) { C 2. x = "알기 쉽게 해설한 자바"; 클래스 이름을 통하여 C 1. x = 30000; 클래스 변수에 값 저장 C 1. y = 20000; System. out. println ("클래스 변수 C 2. x 값 : " + C 2. x); System. out. println ("클래스 변수 C 2. y 값(C 1으로부터 상속) : " + C 2. y); System. out. println ("클래스 변수 C 1. x 값 : " + C 1. x); } } 13
3 메소드의 상속과 오버라이딩(Overriding) l 실습예제 Method. Inheritance. Test. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 8장. 상속 class AAA { int i; int j; void setij(int x, int y) { i = x; j = y; } } class BBB extends AAA { 클래스 AAA로부터 객체 변수와 메소드가 상속 int total; void sum() { total = i + j; } } public class Method. Inheritance. Test { public static void main(String args[]) { BBB sub. Ob = new BBB(); sub. Ob. setij(10, 12); 상속된 메소드 호출 sub. Ob. sum(); System. out. println ("두 수의 합계는 : " + sub. Ob. total); } } 16
3 메소드의 상속과 오버라이딩(Overriding) l 실습예제 Method. Inheritance. Test. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 8장. 상속 class Da { void show(String str) { 매개변수가 있는 show() 메소드 선언 System. out. println ("상위 클래스의 메소드 show(String str)수행 " + str); } } class Db extends Da { 하위 클래스에서 매개변수가 없는 show() 메소드 선언 void show() { System. out. println("하위클래스의 메소드 show() 수행"); } } public class Overriding. Test 1 { public static void main(String args[]) { Db over = new Db(); over. show("알기 쉽게 해설한 자바"); over. show(); } } 18
3 메소드의 상속과 오버라이딩(Overriding) l 실습예제 Overriding. Test 2. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 8장. 상속 class Ea { void show() { 상속된 관계에서 동일한 메소드 선언(Overriding) System. out. println ("상위 클래스의 메소드 show(String str) 수행"); } } class Eb extends Ea { 상속된 관계에서 동일한 메소드 선언(Overriding) void show() { System. out. println("하위클래스의 메소드 show() 수행"); } } public class Overriding. Test 2 { public static void main(String args[]) { Eb over = new Eb(); 하위 클래스로 선언된 show() 메소드 수행 over. show(); } } 20
4 예약어 super l 실습예제 Super. Test 1. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 8장. 상속 class D 1 { int x = 1000; void display() { System. out. println("상위 클래스 D 1의 display() 메소드입니다"); } } class D 2 extends D 1 { int x = 2000; display() 메소드를 오버라이딩 void display() { System. out. println("하위클래스 D 2의 display() 메소드 입니다"); } void write() { display(); super를 사용하여 상위 클래스 메소드 수행 super. display(); System. out. println ("D 2 클래스 객체의 x 값은 : " + x); System. out. println 23
4 예약어 super l 실습예제 Super. Test 1. java 18 19 20 21 22 23 24 25 26 27 8장. 상속 ("D 1 클래스 객체의 x 값은 : " + super. x); super를 사용하여 상위 클래스 객체 변수 사용 } } class Super. Test 1 { public static void main(String args[]) { D 2 d = new D 2(); d. write(); } } 24
5 상속과 생성자 l 실습예제 Super. Test 2. java 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 8장. 상속 class A 3 extends A 2 { double d 3; A 3() { 상속관계의 클래스에서 묵시적 생성자 선언 System. out. println("클래스 A 3의 생성자 수행"); d 3 = 10*10*10*10; } } class Super. Test 2 { public static void main(String args[]) { 하위 클래스로부터 객체 생성. 상위 클래스 묵시적 생성자 수행 A 3 super 1 = new A 3(); System. out. println("10의 2제곱 : " + super 1. d 1); System. out. println("10의 3제곱 : " + super 1. d 2); System. out. println("10의 4제곱 : " + super 1. d 3); } } 28
5 상속과 생성자 l 실습예제 Super. Test 3. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 8장. 상속 class Ad 1 { int d 1; int s; Ad 1(int s 1) { 매개변수가 있는 생성자 선언 System. out. println("클래스 Ad 1의 생성자 수행"); s = s 1; d 1 = s * s ; } } class Ad 2 extends Ad 1 { int d 2; int t; Ad 2(int s 1, int t 1) { 하위 클래스의 생성자 첫 번째 라인에서 상위 클래스 생성자 호출 super(s 1); 30
5 상속과 생성자 l 실습예제 Super. Test 2. java 15 16 17 18 19 20 21 22 23 24 25 26 8장. 상속 System. out. println("클래스 Ad 2의 생성자 수행"); t = t 1; d 2 = t * t ; } } public class Super. Test 3 { public static void main(String args[]) { 객체 생성. 생성자가 수행됨 Ad 2 super 2 = new Ad 2(10, 20); System. out. println("10의 제곱은 : " + super 2. d 1); System. out. println("20의 제곱은 : " + super 2. d 2); } } 31
6 객체의 형변환 l 자바는 클래스 계층 구조에서 상속관계의 클래스로부터 생성된 객체들 사이 의 형변환을 허용 class Acast { int a=1; } class Bcast extends Acast { int b=2; } class Ccast extends Bcast { int c=3; } public class Test. Casting { public static void main(String[] args) { Acast 타입의 객체 ref. A 선언 Acast ref. A; ref. A = new Ccast(); Acast 타입의 객체 변수 ref. A에 System. out. println("ref. A. a의 값은 "+ref. A. a); Ccast 클래스의 객체를 생성하여 할당 } } 8장. 상속 32
6 객체의 형변환 l 메인 클래스를 다음과 같이 수정 public class Test. Casting { public static void main(String[] args) { Acast ref. A; ref. A = new Ccast(); System. out. println("ref. A. a의 값은 "+ref. A. c); } } 8장. 상속 Ccast 클래스 멤버에 접근. 오류 발생 33
6 객체의 형변환 l Ccast형의 객체 변수로 ref. C를 선언하고, ref. C에 Acast형의 객체를 할당 public class Test. Casting { public static void main(String[] args) { Ccast ref. C = new Acast(); 오류 발생 System. out. println("ref. C. a의 값은 "+ref. C. a); } } 8장. 상속 34
6 객체의 형변환 l 실습예제 Overriding. And. Casting. java 01 02 03 04 05 06 07 08 09 10 11 12 13 8장. 상속 class Am { 3개의 상속된 클래스에 메소드 오버라이딩 void callme() { System. out. println ("클래스 Am의 callme() 메소드 실행"); } } class Bm extends Am { 3개의 상속된 클래스에 메소드 오버라이딩 void callme() { System. out. println ("클래스 Bm의 callme() 메소드 실행"); } } 36
6 객체의 형변환 l 실습예제 Overriding. And. Casting. java 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 8장. 상속 class Cm extends Am { 3개의 상속된 클래스에 메소드 오버라이딩 void callme() { System. out. println ("클래스 Cm의 callme() 메소드 실행"); } } public class Overriding. And. Casting { public static void main(String args[]) { Am r = new Am(); Am 클래스의 객체 변수 r에 Am 클래스 객체 배정 r. callme(); Am 클래스의 객체 변수 r에 Bm 클래스 객체 배정 r = new Bm(); r. callme(); Am 클래스의 객체 변수 r에 Cm 클래스 객체 배정 r = new Cm(); r. callme(); } } 37
7 연산자 instanceof l 실습예제 Instanceof. And. Casting. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 8장. 상속 class An { int i, j; } 3개의 클래스가 상속 class Bn extends An{ int k; } class Cn extends Bn { int m; } public class Instanceof. And. Casting { public static void main(String args[]) { An a = new An(); Bn b = new Bn(); Cn c = new Cn(); 40
7 연산자 instanceof l 실습예제 Instanceof. And. Casting. java 15 16 17 18 19 20 21 22 23 24 25 26 27 28 8장. 상속 if(a instanceof An) System. out. println("a는 A 클래스의 객체"); if(b instanceof Bn) System. out. println("b는 B 클래스의 객체"); if(c instanceof Cn) System. out. println("c는 C 클래스의 객체"); 형변환 성공 if(c instanceof An) System. out. println("c는 A 클래스의 객체 : 형변환"); 형변환 불가 if(a instanceof Cn) System. out. println("a는 C 클래스의 객체 : 형변환"); else System. out. println("a는 C 클래스의 객체가 아님 : 형변환 불가"); } } 41
7 연산자 instanceof 8장. 상속 43
8 추상 클래스와 추상 메소드 l 실습예제 Abstract. Test. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 8장. 상속 abstract class Shape { abstract void draw(); abstract void compute. Area(double a, double b); } class Circle extends Shape { void draw() { System. out. println("원을 그리는 기능"); } void compute. Area(double r 1, double r 2) { System. out. println("원의 넓이 : " + (3. 14 * r 1 * r 2)); } } class Rectangle extends Shape { void draw() { System. out. println("사각형을 그리는 기능"); } void compute. Area(double h, double v) { System. out. println("사각형의 넓이 : " + (h * v)); } } 추상 클래스와 추상 메소드 선언 47
8 추상 클래스와 추상 메소드 l 실습예제 Abstract. Test. java 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 8장. 상속 class Triangle extends Shape { void draw() { System. out. println("삼각형을 그리는 기능"); } void compute. Area(double a, double h) { System. out. println("삼각형의 넓이 : " + (a * h / 2)); } } public class Abstract. Test { public static void main(String args[]) { Circle c = new Circle(); c. draw(); c. compute. Area(5. 0, 5. 0); 서로 다른 객체에 같은 이름의 메소드 사용 Rectangle r = new Rectangle(); r. draw(); 서로 다른 객체에 같은 이름의 메소드 사용 r. compute. Area(5. 0, 10. 0); Triangle t = new Triangle(); t. draw(); t. compute. Area(5. 0, 10. 0); 서로 다른 객체에 같은 이름의 메소드 사용 48
8 추상 클래스와 추상 메소드 l 실습예제 Abstract. Test. java 41 42 43 44 45 46 47 48 49 50 51 52 8장. 상속 } } System. out. println("==객체 형변환과 오버라이딩을 이용=="); Shape s = new Circle(); s. draw(); s. compute. Area(5. 0, 5. 0); s = new Rectangle(); 객체의 형변환을 이용하여 동일한 s. draw(); 메소드에 대해 서로 다른 결과 출력 s. compute. Area(5. 0, 10. 0); s = new Triangle(); s. draw(); s. compute. Area(5. 0, 10. 0); 49
- Slides: 57