23 class Sub Class Name extends Super Class



![확장 클래스 [2/3] 확장 클래스 정의 형태 class Sub. Class. Name extends Super. Class. 확장 클래스 [2/3] 확장 클래스 정의 형태 class Sub. Class. Name extends Super. Class.](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-4.jpg)
확장 클래스 [2/3] 확장 클래스 정의 형태 class Sub. Class. Name extends Super. Class. Name { // 필드 선언 // 메소드 정의 } 확장 클래스의 예 : class Super. Class { int a; void method. A { //. . . } } Lecture 6: 확장 클래스 class Sub. Class extends Super. Class { int b; void method. B { //. . . } } 4



![확장 클래스의 생성자 [2/2] 확장 클래스의 생성자 사용 예 [예제 6. 2 - Sub. 확장 클래스의 생성자 [2/2] 확장 클래스의 생성자 사용 예 [예제 6. 2 - Sub.](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-8.jpg)
확장 클래스의 생성자 [2/2] 확장 클래스의 생성자 사용 예 [예제 6. 2 - Sub. Constructor. java] class Super. Class { Super. Class() { System. out. println("Super. Class Constructor. . . "); } } class Sub. Class extends Super. Class { Sub. Class() { System. out. println("Sub. Class Constructor. . . "); } } public class Sub. Constructor { public static void main(String[] args) { Sub. Class obj = new Sub. Class(); System. out. println("in main. . . "); } } 실행 결과 : Super. Class Constructor. . . Sub. Class Constructor. . . in main. . . Lecture 6: 확장 클래스 8

![메소드 재정의 [2/2] 메소드 재정의 예 [예제 6. 6 - Addendum. java] class Super. 메소드 재정의 [2/2] 메소드 재정의 예 [예제 6. 6 - Addendum. java] class Super.](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-10.jpg)
메소드 재정의 [2/2] 메소드 재정의 예 [예제 6. 6 - Addendum. java] class Super. Class { void method. A() { System. out. println("do Super. Class Task. "); } } class Sub. Class extends Super. Class { void method. A() { super. method. A(); System. out. println("do Sub. Class Task. "); } } public class Addendum { public static void main(String[] args) { Sub. Class obj = new Sub. Class(); obj. method. A(); } } 실행 결과 : do Super. Class Task. do Sub. Class Task. Lecture 6: 확장 클래스 10


![추상 클래스(abstract class) [3/3] 추상 클래스 사용 예 [예제 6. 7 - Abstract. Class. 추상 클래스(abstract class) [3/3] 추상 클래스 사용 예 [예제 6. 7 - Abstract. Class.](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-13.jpg)
추상 클래스(abstract class) [3/3] 추상 클래스 사용 예 [예제 6. 7 - Abstract. Class. Test. java] abstract class Abstract. Class { public abstract void method. A(); void method. B() { System. out. println("Implementation of method. B()"); } } class Imp. Class extends Abstract. Class { public void method. A () { System. out. println("Implementation of method. A()"); } } public class Abstract. Class. Test { public static void main(String[] args) { Imp. Class obj = new Imp. Class(); obj. method. A(); obj. method. B(); } } 실행 결과 : Implementation of method. A() Implementation of method. B() Lecture 6: 확장 클래스 13

![무명 클래스(Anonymous Class) [2/2] 무명 클래스 사용 예 [예제 6. 8 - Anonymous. Example. 무명 클래스(Anonymous Class) [2/2] 무명 클래스 사용 예 [예제 6. 8 - Anonymous. Example.](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-15.jpg)
무명 클래스(Anonymous Class) [2/2] 무명 클래스 사용 예 [예제 6. 8 - Anonymous. Example. java] class Anonymous. Class { public void print() { System. out. println("This is Anonym. Test Class. "); } } public class Anonymous. Example { public static void method. A(Anonymous. Class obj) { obj. print(); } public static void main(String[] args) { method. A(new Anonymous. Class() { // 무명 클래스 public void print() { System. out. println("This is redefined by Anonymous Example. "); } } 실행 결과 : This is redefined by Anonymous Example. Lecture 6: 확장 클래스 15

![인터페이스 선언 [1/4] 선언 형태 [public] interface. Name [extends List. Of. Super. Interfaces] // 인터페이스 선언 [1/4] 선언 형태 [public] interface. Name [extends List. Of. Super. Interfaces] //](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-17.jpg)
인터페이스 선언 [1/4] 선언 형태 [public] interface. Name [extends List. Of. Super. Interfaces] // constant definitions // method declarations } 사용 예 interface Base. Colors { int RED = 1; int GREEN = 2; int BLUE = 4; } 내부적으로 상수를 의미하는 public, static, final의 속성 void set. Color(int color); int get. Color(); Lecture 6: 확장 클래스 17
![인터페이스 선언 [2/4] 인터페이스의 필드 내부적으로 상수를 의미하는 public, static, final의 속성 선언된 모든 인터페이스 선언 [2/4] 인터페이스의 필드 내부적으로 상수를 의미하는 public, static, final의 속성 선언된 모든](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-18.jpg)
인터페이스 선언 [2/4] 인터페이스의 필드 내부적으로 상수를 의미하는 public, static, final의 속성 선언된 모든 필드는 반드시 초기화 interface Base. Colors { int RED = 1; int GREEN = RED + 1; int BLUE = GREEN + 2; C 언어의 #define문 사용과 유사 void set. Color(int color); int get. Color(); } Lecture 6: 확장 클래스 18


![인터페이스 확장 [1/4] 확장 형태 [public] interface. Name extends List. Of. Super. Interfaces { 인터페이스 확장 [1/4] 확장 형태 [public] interface. Name extends List. Of. Super. Interfaces {](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-21.jpg)
인터페이스 확장 [1/4] 확장 형태 [public] interface. Name extends List. Of. Super. Interfaces { // constant definitions // method declarations } 확장 예 interface Rainbow. Colors extends Base. Colors { int YELLOW = 3; int ORANGE = 5; int INDIGO = 6; int VIOLET = 7; void print. Color(int color); } Lecture 6: 확장 클래스 21

![인터페이스 확장 [3/4] 다중 상속 예 interface Many. Colors extends Rainbow. Colors, Print. Colors 인터페이스 확장 [3/4] 다중 상속 예 interface Many. Colors extends Rainbow. Colors, Print. Colors](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-23.jpg)
인터페이스 확장 [3/4] 다중 상속 예 interface Many. Colors extends Rainbow. Colors, Print. Colors { int VERMILION = 3; int CHARTUSE = RED + 90; } 다중 상속시 동일한 이름의 상수를 상속 단순 명은 ambiguous하기 때문에 에러 Rainbow. Colors. YELLOW, Print. Colors. YELLOW Lecture 6: 확장 클래스 23
![인터페이스 확장 [4/4] 메소드 상속 overloading, overriding 시그네춰가 같고 복귀형이 다르면 에러 메소드 상속 인터페이스 확장 [4/4] 메소드 상속 overloading, overriding 시그네춰가 같고 복귀형이 다르면 에러 메소드 상속](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-24.jpg)
인터페이스 확장 [4/4] 메소드 상속 overloading, overriding 시그네춰가 같고 복귀형이 다르면 에러 메소드 상속 예 interface Base. Colors { //. . . void set. Color(int color); int get. Color(); } interface Action. Colors extends Base. Colors { void set. Color(int color) ; // overriding void set. Color(int red, int green, int blue) ; // overloading // Color get. Color(); // 에러 } Lecture 6: 확장 클래스 24
![인터페이스 구현 [1/7] 클래스를 통하여 구현된 후 객체를 가짐 구현 형태 class Class. Name 인터페이스 구현 [1/7] 클래스를 통하여 구현된 후 객체를 가짐 구현 형태 class Class. Name](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-25.jpg)
인터페이스 구현 [1/7] 클래스를 통하여 구현된 후 객체를 가짐 구현 형태 class Class. Name implements Interface. Name { // fields // methods } Lecture 6: 확장 클래스 25
![인터페이스 구현 [2/7] 구현 예 class Color implements Base. Colors { // fields public 인터페이스 구현 [2/7] 구현 예 class Color implements Base. Colors { // fields public](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-26.jpg)
인터페이스 구현 [2/7] 구현 예 class Color implements Base. Colors { // fields public void set. Color(int color) { // 몸체 완성 } public int get. Color() { // 몸체 완성 } } interface Base. Colors { int RED = 1; int GREEN = RED + 1; int BLUE = GREEN + 2; void set. Color(int color); int get. Color(); } 구현시 public 명시 Lecture 6: 확장 클래스 26

![인터페이스 구현 [4/7] 클래스 확장과 동시에 인터페이스 구현 class Class. Name extends Super. Class 인터페이스 구현 [4/7] 클래스 확장과 동시에 인터페이스 구현 class Class. Name extends Super. Class](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-28.jpg)
인터페이스 구현 [4/7] 클래스 확장과 동시에 인터페이스 구현 class Class. Name extends Super. Class implements List. Of. Interfaces { // fields // methods } 다이아몬드 상속 [예제 6. 10] 테스트 interface W { } interface X extends W { } class Y implements W { } class Z extends Y implements X { } Lecture 6: 확장 클래스 28
![인터페이스 구현 [5/7] 인터페이스 구현의 예 – 정의 및 구현 [예제 6. 10 - 인터페이스 구현 [5/7] 인터페이스 구현의 예 – 정의 및 구현 [예제 6. 10 -](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-29.jpg)
인터페이스 구현 [5/7] 인터페이스 구현의 예 – 정의 및 구현 [예제 6. 10 - Implementing. Interface. java] interface Base. Colors { int RED = 1, GREEN = 2, BLUE = 4; void set. Color(int color); int get. Color(); } abstract class Set. Color implements Base. Colors { protected int color; public void set. Color(int color) { this. color = color; System. out. println("in the set. Color method. . . "); } } class Color extends Set. Color { public int get. Color() { System. out. println("in the get. Color method. . . "); return color; } } [Next Page] Lecture 6: 확장 클래스 29
![인터페이스 구현 [6/7] 인터페이스 구현의 예 – 사용 [예제 6. 10 - Implementing. Interface. 인터페이스 구현 [6/7] 인터페이스 구현의 예 – 사용 [예제 6. 10 - Implementing. Interface.](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-30.jpg)
인터페이스 구현 [6/7] 인터페이스 구현의 예 – 사용 [예제 6. 10 - Implementing. Interface. java](cont. ) public class Implementing. Interface { public static void main(String[] args) { Color c = new Color(); int i; c. set. Color(10); i = c. get. Color(); System. out. println("in the main method. . . "); } } 실행 결과 : in the set. Color method. . . in the get. Color method. . . in the main method. . . Lecture 6: 확장 클래스 30


![클래스형 변환 [1/3] 슈퍼클래스 cast 연산자 자동 변환 서브클래스 casting up : valid type 클래스형 변환 [1/3] 슈퍼클래스 cast 연산자 자동 변환 서브클래스 casting up : valid type](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-33.jpg)
클래스형 변환 [1/3] 슈퍼클래스 cast 연산자 자동 변환 서브클래스 casting up : valid type conversion casting down : invalid type conversion Lecture 6: 확장 클래스 33
![클래스형 변환 [2/3] void dummy(CLanguage obj) { // … } //. . . Java 클래스형 변환 [2/3] void dummy(CLanguage obj) { // … } //. . . Java](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-34.jpg)
클래스형 변환 [2/3] void dummy(CLanguage obj) { // … } //. . . Java j = new Java(); dummy(j); // OK void dummy(Java obj) { // … } //. . . Clanguage c = new CLanguage(); dummy(c); // 에러 dummy( (Java)c ); // 예외발생 Lecture 6: 확장 클래스 34


![클래스 설계 [2/2] class Ansi. C { int declarations; int operators; int statements; void 클래스 설계 [2/2] class Ansi. C { int declarations; int operators; int statements; void](http://slidetodoc.com/presentation_image_h2/5b25c6d5559d28a6197b1465f15ead0d/image-37.jpg)
클래스 설계 [2/2] class Ansi. C { int declarations; int operators; int statements; void functions() { //. . . } } class Java extends Ansi. C { int classes; int exceptions; int threads; } class Cplus Extends Ansi. C { int classes; int exceptions; int operator. Overloadings; } Lecture 6: 확장 클래스 class Oopl extends Ansi. C { int classes; int exceptions; } class Java extends Oopl { int threads; } class Cplus extends Oopl { int operator. Overloadings; } 37
- Slides: 37