PowerPoint Presentation

Similar documents
Design Issues

PowerPoint Presentation

PowerPoint Presentation

gnu-lee-oop-kor-lec06-3-chap7

PowerPoint Presentation

JAVA PROGRAMMING 실습 08.다형성

q 이장에서다룰내용 1 객체지향프로그래밍의이해 2 객체지향언어 : 자바 2

PowerPoint Presentation

쉽게 풀어쓴 C 프로그래밍

PowerPoint Presentation

PowerPoint 프레젠테이션

<4D F736F F F696E74202D20C1A63038C0E520C5ACB7A1BDBABFCD20B0B4C3BC4928B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

Microsoft PowerPoint - 2강

No Slide Title

PowerPoint 프레젠테이션

PowerPoint Presentation

JAVA PROGRAMMING 실습 07. 상속

JAVA PROGRAMMING 실습 02. 표준 입출력

PowerPoint 프레젠테이션

PowerPoint Presentation

제11장 프로세스와 쓰레드

JAVA 프로그래밍실습 실습 1) 실습목표 - 메소드개념이해하기 - 매개변수이해하기 - 새메소드만들기 - Math 클래스의기존메소드이용하기 ( ) 문제 - 직사각형모양의땅이있다. 이땅의둘레, 면적과대각

JAVA PROGRAMMING 실습 05. 객체의 활용

슬라이드 1

PowerPoint Presentation

Microsoft PowerPoint - additional08.ppt [호환 모드]

C++ Programming

10.0pt1height.7depth.3width±â10.0pt1height.7depth.3widthÃÊ10.0pt1height.7depth.3widthÅë10.0pt1height.7depth.3width°è10.0pt1height.7depth.3widthÇÁ10.0pt1height.7depth.3width·Î10.0pt1height.7depth.3width±×10.0pt1height.7depth.3width·¡10.0pt1height.7depth.3width¹Ö pt1height.7depth.3widthŬ10.0pt1height.7depth.3width·¡10.0pt1height.7depth.3width½º, 10.0pt1height.7depth.3width°´10.0pt1height.7depth.3widthü, 10.0pt1height.7depth.3widthº¯10.0pt1height.7depth.3width¼ö, 10.0pt1height.7depth.3width¸Þ10.0pt1height.7depth.3width¼Ò10.0pt1height.7depth.3widthµå

(Microsoft PowerPoint - java2-lecture3.ppt [\310\243\310\257 \270\360\265\345])

쉽게 풀어쓴 C 프로그래밍

PowerPoint Presentation

PowerPoint Presentation

쉽게 풀어쓴 C 프로그래밍

Microsoft PowerPoint 자바-기본문법(Ch2).pptx

예제 2) Test.java class A intvar= 10; void method() class B extends A intvar= 20; 1"); void method() 2"); void method1() public class Test 3"); args) A

슬라이드 1

Microsoft PowerPoint - C++ 5 .pptx

Microsoft PowerPoint - Lect04.pptx

JAVA PROGRAMMING 실습 05. 객체의 활용

Microsoft PowerPoint - Java7.pptx

JAVA PROGRAMMING 실습 09. 예외처리

(Microsoft PowerPoint - 07\300\345.ppt [\310\243\310\257 \270\360\265\345])

5장.key

설계란 무엇인가?

PowerPoint 프레젠테이션

쉽게

17장 클래스와 메소드

Microsoft PowerPoint - java1-lab5-ImageProcessorTestOOP.pptx

쉽게 풀어쓴 C 프로그래밍

(Microsoft PowerPoint - java1-lecture11.ppt [\310\243\310\257 \270\360\265\345])

1. 객체의생성과대입 int 형변수 : 선언과동시에초기화하는방법 (C++) int a = 3; int a(3); // 기본타입역시클래스와같이처리가능 객체의생성 ( 복습 ) class CPoint private : int x, y; public : CPoint(int a

Slide 1

PowerPoint 프레젠테이션

쉽게 풀어쓴 C 프로그래밍

오버라이딩 (Overriding)

PowerPoint Presentation

Microsoft PowerPoint - chap11

(8) getpi() 함수는정적함수이므로 main() 에서호출할수있다. (9) class Circle private double radius; static final double PI= ; // PI 이름으로 로초기화된정적상수 public

JVM 메모리구조

PowerPoint Template

PowerPoint Presentation

1. auto_ptr 다음프로그램의문제점은무엇인가? void func(void) int *p = new int; cout << " 양수입력 : "; cin >> *p; if (*p <= 0) cout << " 양수를입력해야합니다 " << endl; return; 동적할

PowerPoint Presentation

PowerPoint Presentation

Microsoft PowerPoint - java2 [호환 모드]

Microsoft PowerPoint 장강의노트.ppt

슬라이드 1

PowerPoint 프레젠테이션

Java ...

PowerPoint 프레젠테이션

<4D F736F F F696E74202D2036C0CFC2B05FB0B4C3BCC1F6C7E2C7C1B7CEB1D7B7A1B9D62E707074>

Slide 1

슬라이드 1

Microsoft PowerPoint - chap06-2pointer.ppt

4 장클래스와객체 클래스와객체 public과 private 구조체와클래스객체의생성과생성자객체의소멸과소멸자생성자와소멸자의호출순서디폴트생성자와디폴트소멸자멤버초기화멤버함수의외부정의멤버함수의인라인함수선언 C++ 프로그래밍입문

PowerPoint 프레젠테이션

Microsoft Word - java19-1-midterm-answer.doc


Microsoft PowerPoint - Chapter 6.ppt

PowerPoint Presentation

Microsoft Word - EEL2 Lab4.docx

No Slide Title

Network Programming

PowerPoint Template

Spring Boot/JDBC JdbcTemplate/CRUD 예제

JAVA PROGRAMMING 실습 02. 표준 입출력

Cluster management software

슬라이드 1

Microsoft PowerPoint - CSharp-10-예외처리

PowerPoint 프레젠테이션

Cluster management software

<4A DB0B4C3BCC1F6C7E2C7C1B7CEB1D7B7A1B9D6BEF0BEEE2E687770>

쉽게 풀어쓴 C 프로그래밍

PowerPoint 프레젠테이션

Microsoft PowerPoint - Chap12-OOP.ppt

Microsoft PowerPoint - 04-UDP Programming.ppt

JAVA PROGRAMMING 실습 02. 표준 입출력

자바 프로그래밍

C++ Programming

[ 마이크로프로세서 1] 2 주차 3 차시. 포인터와구조체 2 주차 3 차시포인터와구조체 학습목표 1. C 언어에서가장어려운포인터와구조체를설명할수있다. 2. Call By Value 와 Call By Reference 를구분할수있다. 학습내용 1 : 함수 (Functi

Transcription:

public class SumTest { public static void main(string a1[]) { int a, b, sum; a = Integer.parseInt(a1[0]); b = Integer.parseInt(a1[1]); sum = a + b ; // 두수를더하는부분입니다 System.out.println(" 두수의합은 " + sum + " 입니다 "); 1

학습목표 클래스의계층구조와상속에관해학습한다. 상속관계의클래스에서멤버변수의상속에관해학습한다. 상속관계의클래스에서메소드의치환 (Overriding) 에관해학습한다. 상속관계의클래스에서생성자의사용에관해학습한다. 상속관계에서객체의형변환에관해학습한다. 예약어 super, final 연산자 instanceof 에관해학습한다. 2

목차 section 1 상속 (Inheritance) 의개요 section 2 멤버변수의상속 section 3 메소드의상속과오버라이딩 (Overriding) section 4 예약어 super section 5 속과생성자 section 6 객체의형변환 section 7 연산자 instanceof section 8 추상클래스와추상메소드 section 9 예약어 final 3

1 상속 (Inheritance) 의개요 상속 (inheritance) - 객체지향언어의장점인모듈의재사용 (reusing) 과코드의간결성을제공하는중요한특성 형식 [public/final/abstract] class 클래스이름 extends 상위클래스이름 {... 멤버변수선언... 생성자선언... 메소드선언 public class Box extends SuperBox {... public class MyApplet extends Applet {... 4

2 멤버변수의상속 protected 접근한정자 - 같은패키지내의클래스와같은패키지는아니지만상속된클래스에서사용가능한접근 한정자 멤버변수의접근한정자 - public : 동일한패키지인지, 상속관계인지에상관없이모든클래스에서사용가능 - 한정자없음 : 동일한패키지이면상속여부에상관없이사용가능 - protected : 동일한패키지이면상속여부에상관없이사용가능하며, 다른패키지라도상속되었으면사용가능 - private : 어떠한경우에도사용불가능. 클래스내부에서만사용가능 5

2 멤버변수의상속 접근한정자의사용 247 page 6

2 멤버변수의상속 실습예제 InheritanceTest1.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 class A { int aa = 1; class B extends A { int bb = 2; class C extends B { int cc = 3; 세개의클래스가상속관계 public class InheritanceTest1 { 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 멤버변수의상속 8

2 멤버변수의상속 실습예제 InheritanceTest2.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 class AA { int i; private int j; void setij(int x, int y) { i = x; j = y; class BB extends AA { int total; void sum() { total = i + j; 오류발생 public class InheritanceTest2 { public static void main(string args[]) { BB subob = new BB(); subob.sum(); 객체변수 j 를 private 로선언 AA 클래스로부터상속받아 BB 클래스생성 9

2 멤버변수의상속 실습예제 InheritanceTest3.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 class B1 { int x; 객체변수 x 선언 class B2 extends B1 { String x; public class InheritanceTest3 { public static void main(string args[]) { B2 b2 = new B2(); b2.x = " 알기쉽게해설한자바 "; System.out.println(" 객체 b2에들어있는 x 값 : " + b2.x); B1 b1 = new B1(); b1.x = 5000; x는상위클래스에서선언된정수 System.out.println(" 객체 b1에들어있는 x 값 : " + b1.x); 객체변수 x 선언. 상위클래스의객체변수 x 가가려짐 x 는하위클래스에서선언된문자열 10

2 멤버변수의상속 실습예제 InheritanceTest4.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 class C1 { static int x; static int y; class C2 extends C1 { static String x; class InheritanceTest4 { public static void main(string args[]) { C2.x = " 알기쉽게해설한자바 "; C1.x = 30000; C1.y = 20000; System.out.println (" 클래스변수 C2.x 값 : " + C2.x); System.out.println 두개의클래스변수선언 상속된클래스에서같은이름을가진클래스변수선언 클래스이름을통하여클래스변수에값저장 (" 클래스변수 C2.y 값 (C1 으로부터상속 ) : " + C2.y); System.out.println (" 클래스변수 C1.x 값 : " + C1.x); 11

2 멤버변수의상속 12

3 메소드의상속과오버라이딩 (Overriding) 오버라이딩 (overriding) - 상속관계에있는클래스들간에같은이름의메소드를정의하는경우 - 상위클래스의메소드와하위클래스의메소드가메소드이름은물론매개변수의타입과개수까지도같아야함 13

3 메소드의상속과오버라이딩 (Overriding) 실습예제 MethodInheritanceTest.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 MethodInheritanceTest { public static void main(string args[]) { BBB subob = new BBB(); subob.setij(10, 12); 상속된메소드호출 subob.sum(); System.out.println (" 두수의합계는 : " + subob.total); 14

3 메소드의상속과오버라이딩 (Overriding) 15

3 메소드의상속과오버라이딩 (Overriding) 실습예제 MethodInheritanceTest.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 class Da { void show(string str) { System.out.println (" 상위클래스의메소드 show(string str) 수행 " + str); class Db extends Da { void show() { System.out.println(" 하위클래스의메소드 show() 수행 "); public class OverridingTest1 { public static void main(string args[]) { Db over = new Db(); over.show(" 알기쉽게해설한자바 "); over.show(); 매개변수가있는 show() 메소드선언 하위클래스에서매개변수가없는 show() 메소드선언 16

3 메소드의상속과오버라이딩 (Overriding) 17

3 메소드의상속과오버라이딩 (Overriding) 실습예제 OverridingTest2.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 class Ea { void show() { System.out.println (" 상위클래스의메소드 show(string str) 수행 "); class Eb extends Ea { void show() { System.out.println(" 하위클래스의메소드 show() 수행 "); public class OverridingTest2 { public static void main(string args[]) { Eb over = new Eb(); over.show(); 상속된관계에서동일한메소드선언 (Overriding) 상속된관계에서동일한메소드선언 (Overriding) 하위클래스로선언된 show() 메소드수행 18

3 메소드의상속과오버라이딩 (Overriding) 19

4 예약어 super 예약어 super - 하위클래스에의해가려진상위클래스의멤버변수나메소드에접근할때사용 - 상위클래스의생성자를호출하기위해사용 형식 super. 객체변수 super. 메소드이름 ( 매개변수 ) 20

4 예약어 super 실습예제 SuperTest1.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 class D1 { int x = 1000; void display() { System.out.println(" 상위클래스 D1 의 display() 메소드입니다 "); class D2 extends D1 { int x = 2000; void display() { System.out.println(" 하위클래스 D2 의 display() 메소드입니다 "); void write() { display(); super.display(); System.out.println ("D2 클래스객체의 x 값은 : " + x); System.out.println display() 메소드를오버라이딩 super 를사용하여상위클래스메소드수행 21

4 예약어 super 실습예제 SuperTest1.java 18 19 20 21 22 23 24 25 26 27 ("D1 클래스객체의 x 값은 : " + super.x); class SuperTest1 { public static void main(string args[]) { D2 d = new D2(); d.write(); super 를사용하여상위클래스객체변수사용 22

4 예약어 super 23

5 상속과생성자 묵시적 (default) 생성자 - 생성자중에매개변수가없는생성자 super 문장 - 상위클래스의특정생성자를호출하는문장 - 반드시생성자부분의첫번째라인에위치 - 상위클래스의생성자가항상하위클래스생성자보다먼저수행되어야함을의미 super- 상위클래스생성자호출 형식 super( 매개변수 ) 24

5 상속과생성자 실습예제 SuperTest2.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 class A1 { double d1; A1() { System.out.println(" 클래스 A1 의생성자수행 "); d1 = 10*10; class A2 extends A1 { double d2; A2() { System.out.println(" 클래스 A2 의생성자수행 "); d2 = 10*10*10; 상속관계의클래스에서묵시적생성자선언 상속관계의클래스에서묵시적생성자선언 25

5 상속과생성자 실습예제 SuperTest2.java 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 class A3 extends A2 { double d3; A3() { System.out.println(" 클래스 A3 의생성자수행 "); d3 = 10*10*10*10; class SuperTest2 { public static void main(string args[]) { A3 super1 = new A3(); 상속관계의클래스에서묵시적생성자선언 System.out.println("10 의 2 제곱 : " + super1.d1); System.out.println("10 의 3 제곱 : " + super1.d2); System.out.println("10 의 4 제곱 : " + super1.d3); 하위클래스로부터객체생성. 상위클래스묵시적생성자수행 26

5 상속과생성자 27

5 상속과생성자 실습예제 SuperTest3.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 class Ad1 { int d1; int s; Ad1(int s1) { System.out.println(" 클래스 Ad1 의생성자수행 "); s = s1; d1 = s * s ; class Ad2 extends Ad1 { int d2; int t; Ad2(int s1, int t1) { super(s1); 매개변수가있는생성자선언 하위클래스의생성자첫번째라인에서상위클래스생성자호출 28

5 상속과생성자 실습예제 SuperTest2.java 15 16 17 18 19 20 21 22 23 24 25 26 System.out.println(" 클래스 Ad2 의생성자수행 "); t = t1; d2 = t * t ; public class SuperTest3 { public static void main(string args[]) { Ad2 super2 = new Ad2(10,20); System.out.println("10 의제곱은 : " + super2.d1); System.out.println("20 의제곱은 : " + super2.d2); 객체생성. 생성자가수행됨 클래스 Ad1 의생성자수행클래스 Ad2 의생성자수행 10 의제곱은 : 100 20 의제곱은 : 400 29

6 객체의형변환 자바는클래스계층구조에서상속관계의클래스로부터생성된객체들사이 의형변환을허용 class Acast { int a=1; class Bcast extends Acast { int b=2; class Ccast extends Bcast { int c=3; public class TestCasting { public static void main(string[] args) { Acast 타입의객체 refa 선언 Acast refa; refa = new Ccast(); Acast 타입의객체변수 refa에 System.out.println("refA.a의값은 "+refa.a); Ccast 클래스의객체를생성하여할당 30

6 객체의형변환 메인클래스를다음과같이수정 public class TestCasting { public static void main(string[] args) { Acast refa; refa = new Ccast(); System.out.println("refA.a 의값은 "+refa.c); Ccast 클래스멤버에접근. 오류발생 31

6 객체의형변환 Ccast 형의객체변수로 refc 를선언하고, refc 에 Acast 형의객체를할당 public class TestCasting { public static void main(string[] args) { Ccast refc = new Acast(); System.out.println("refC.a의값은 "+refc.a); 오류발생 32

6 객체의형변환 클래스사이에서객체의형변환 - 하위클래스에서생성된객체를상위클래스형의객체변수에배정하는형변환은허용 - 반대로상위클래스에서생성된객체를하위클래스형의객체변수에배정할수없다. - 상위클래스형의객체변수에배정된하위클래스객체의경우, 상위클래스형의객체변수를통해서는상위클래스에선언된속성에만접근이가능 33

6 객체의형변환 실습예제 OverridingAndCasting.java 01 02 03 04 05 06 07 08 09 10 11 12 13 class Am { void callme() { System.out.println (" 클래스 Am 의 callme() 메소드실행 "); class Bm extends Am { void callme() { System.out.println 3 개의상속된클래스에메소드오버라이딩 (" 클래스 Bm 의 callme() 메소드실행 "); 3 개의상속된클래스에메소드오버라이딩 34

6 객체의형변환 실습예제 OverridingAndCasting.java 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 class Cm extends Am { void callme() { System.out.println (" 클래스 Cm 의 callme() 메소드실행 "); public class OverridingAndCasting { public static void main(string args[]) { Am r = new Am(); r.callme(); r = new Bm(); r.callme(); r = new Cm(); r.callme(); 3 개의상속된클래스에메소드오버라이딩 Am 클래스의객체변수 r 에 Am 클래스객체배정 Am 클래스의객체변수 r 에 Bm 클래스객체배정 Am 클래스의객체변수 r 에 Cm 클래스객체배정 35

6 객체의형변환 36

7 연산자 instanceof instanceof - 객체가특정클래스나인터페이스로부터생성된객체인지를판별하여 ture 또는 false 값을 반환하여주는이진연산자 형식 객체변수 instanceof type type : 클래스이름또는인터페이스이름 if (oba instanceof String) System.out.println("oba 는 String 클래스의객체입니다 "); 37

7 연산자 instanceof 실습예제 InstanceofAndCasting.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 class An { int i, j; class Bn extends An{ int k; class Cn extends Bn { int m; public class InstanceofAndCasting { public static void main(string args[]) { An a = new An(); Bn b = new Bn(); Cn c = new Cn(); 3 개의클래스가상속 38

7 연산자 instanceof 실습예제 InstanceofAndCasting.java 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 클래스의객체가아님 : 형변환불가 "); 39

7 연산자 instanceof 40

8 추상클래스와추상메소드 추상클래스 - 하위클래스에서구현되는추상적인기능만을정의하는클래스 - 추상클래스에서정의된추상적인기능은하위클래스에서구현 271 page 41

8 추상클래스와추상메소드 추상메소드 - 추상클래스내에정의되는메소드로써선언부분만있고구현부분이없는메소드 - 하위클래스에서는상위클래스에서추상메소드로정의된메소드를오버라이딩하여사용 추상클래스와추상메소드 형식 abstract class 클래스이름 {... 클래스에기술할수있는일반적인멤버변수와메소드... abstract void 추상메소드이름 (); 추상메소드는선언부분만있다. 하위클래스에서오버라이딩하여사용 42

8 추상클래스와추상메소드 추상클래스와추상메소드 abstract class Shape {... abstract void draw();... public class Circle entends Shape {... void draw() { 실제원을그리는기능이기술됨 ; public class Triangle entends Shape {... void draw() { 실제삼각형을그리는기능이기술됨 ; 같은이름의메소드를선언함으로서오버라이딩 43

8 추상클래스와추상메소드 실습예제 AbstractTest.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 abstract class Shape { abstract void draw(); abstract void computearea(double a, double b); class Circle extends Shape { void draw() { System.out.println(" 원을그리는기능 "); void computearea(double r1, double r2) { System.out.println(" 원의넓이 : " + (3.14 * r1 * r2)); class Rectangle extends Shape { void draw() { System.out.println(" 사각형을그리는기능 "); void computearea(double h, double v) { System.out.println(" 사각형의넓이 : " + (h * v)); 추상클래스와추상메소드선언 44

8 추상클래스와추상메소드 실습예제 AbstractTest.java 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 class Triangle extends Shape { void draw() { System.out.println(" 삼각형을그리는기능 "); void computearea(double a, double h) { System.out.println(" 삼각형의넓이 : " + (a * h / 2)); public class AbstractTest { public static void main(string args[]) { Circle c = new Circle(); c.draw(); c.computearea(5.0, 5.0); Rectangle r = new Rectangle(); r.draw(); r.computearea(5.0, 10.0); Triangle t = new Triangle(); t.draw(); t.computearea(5.0, 10.0); 서로다른객체에같은이름의메소드사용 서로다른객체에같은이름의메소드사용 서로다른객체에같은이름의메소드사용 45

8 추상클래스와추상메소드 실습예제 AbstractTest.java 41 42 43 44 45 46 47 48 49 50 51 52 System.out.println("== 객체형변환과오버라이딩을이용 =="); Shape s = new Circle(); s.draw(); s.computearea(5.0, 5.0); s = new Rectangle(); s.draw(); s.computearea(5.0, 10.0); s = new Triangle(); s.draw(); s.computearea(5.0, 10.0); 객체의형변환을이용하여동일한메소드에대해서로다른결과출력 46

8 추상클래스와추상메소드 47

9 예약어 final Final - 자바에서 3가지로사용 - 객체변수에 final을붙여상수로사용 - 상속과관련되어클래스와메소드에 final을붙여사용 메소드에 final 을붙여사용 class A { final void calculate() { System.out.println(" 이메소드는 final 메소드 "); class B extends A { void calculate() { System.out.println("final 메소드를중첩하는메소드 "); 오류발생. 상위클래스에서 final 로선언된메소드는오버라이딩금지 48

9 예약어 final 클래스에도 final 을붙여사용 final class A {... class B extends A {... 오류발생 49

학습정리 상속의개요 1 상속은객체지향의주요특성인모듈의재사용과코드의간결성을제공한다. 2 자바에서모든프로그램은상위클래스를가진다. 3 상속은확장의개념으로상위클래스의모든요소를상속받고추가요소를더가지는개념이다. 멤버변수의상속 1 클래스가상속되면상위클래스에선언된멤버변수는접근한정자에따라상속여부가결정된다. 2 protected 접근한정자는같은패키지내의클래스와같은패키지는아니지만, 상속된클래스에서사용가능한접근한정자이다. 50

학습정리 메소드의상속과오버라이딩 1 클래스가상속되면상위클래스에선언된메소드도접근한정자에따라상속여부가결정된다. 2 상위클래스에서선언된메소드와똑같은메소드를하위클래스에선언하는것을오버라이딩이라한다. 3 상속관계에서메소드가오버라이딩되면상위클래스의메소드는가려지게된다. 예약어 super 1 super는하위클래스에의해가려진상위클래스의멤버변수나메소드에접근할때사용한다. 2 상위클래스의생성자를호출할때도사용한다. 51

학습정리 상속과생성자 1 상속관계에서매개변수가없는생성자 ( 묵시적생성자 ) 는하위클래스에서객체가생성될때자동으로수행된다. 2 묵시적생성자가아닌경우에는명시적으로 super를사용하여호출하여야한다. 객체의형변환 1 상속관계의클래스로부터생성된객체들사이의형변환을허용한다. 2 상위클래스형으로선언된객체변수에하위클래스로부터생성된객체를배정할수있으며, 이경우상위클래스에선언된요소만접근이가능하다. 추상클래스와추상메소드 1 추상클래스는하위클래스에서구현되는추상적인기능만을정의하는클래스로서추상메소드를가지는클래스이다. 2 추상클래스는기능이무엇 (what) 인지만정의하고, 어떻게 (how) 구현되어있는지는정의하지않는다. 어떻게구현되었는지는하위클래스에서정의한다. 52

53