Presentation is loading. Please wait.

Presentation is loading. Please wait.

명품 JAVA Essential.

Similar presentations


Presentation on theme: "명품 JAVA Essential."— Presentation transcript:

1 명품 JAVA Essential

2 학습 목표 객체 지향 상속과 자바 상속 개념 이해 클래스 상속 작성 및 객체 생성 protected 접근 지정
상속 시 생성자의 실행 과정 업캐스팅과 instanceof 연산자 메소드 오버라이딩과 동적 바인딩의 이해 및 활용 추상 클래스 인터페이스

3 상속 (inheritance) 객체 지향 상속 자식이 부모 유전자를 물려 받는 것과 유사한 개념

4 상속의 필요성 상속이 없는 경우 중복된 멤버를 가진 4 개의 클래스 상속을 이용한 경우 중복이 제거되고 간결해진 클래스 구조

5 클래스 상속과 객체 상속 선언 extends 키워드로 선언 부모 클래스 -> 슈퍼 클래스(super class)
부모 클래스를 물려받아 확장한다는 의미 부모 클래스 -> 슈퍼 클래스(super class) 자식 클래스 -> 서브 클래스(sub class) ColorPoint는 Point를 물려 받으므로, Point에 선언된 필드와 메소드 선언 필 요 없음 class Point { int x, y; ... } class ColorPoint extends Point { // Point를 상속받는 ColorPoint 클래스 선언 서브 클래스 슈퍼 클래스

6 예제 5-1 : 클래스 상속 (x, y)의 한 점을 표현하는 Point 클래스와 이를 상속받아 점에 색을 추가한 ColorPoint 클래스를 만들고 활용해보자. class Point { private int x, y; // 한 점을 구성하는 x, y 좌표 void set(int x, int y) { this.x = x; this.y = y; } void showPoint() { // 점의 좌표 출력 System.out.println("(" + x + "," + y + ")"); // Point를 상속받은 ColorPoint 선언 class ColorPoint extends Point { private String color; // 점의 색 void setColor(String color) { this.color = color; void showColorPoint() { // 컬러 점의 좌표 출력 System.out.print(color); showPoint(); // Point의 showPoint() 호출 public class ColorPointEx { public static void main(String [] args) { Point p = new Point(); // Point 객체 생성 p.set(1, 2); // Point 클래스의 set() 호출 p.showPoint(); ColorPoint cp = new ColorPoint(); cp.set(3, 4); // Point 클래스의 set() 호출 cp.setColor("red"); // ColorPoint의 setColor() 호출 cp.showColorPoint(); // 컬러와 좌표 출력 } (1,2) red(3,4)

7 서브 클래스 객체의 모양 슈퍼 클래스 객체와 서브 클래스의 객체는 별개 서브 클래스 객체는 슈퍼 클래스 멤버 포함

8 서브 클래스에서 슈퍼 클래스 멤버 접근

9 자바 상속의 특징 클래스 다중 상속(multiple inheritance) 불허
자바는 인터페이스(interface)의 다중 상속 허용 모든 자바 클래스는 묵시적으로 Object클래스 상속받음 java.lang.Object는 클래스는 모든 클래스의 슈퍼 클래스

10 슈퍼 클래스의 멤버에 대한 서브 클래스의 접근 슈퍼 클래스의 private 멤버 슈퍼 클래스의 디폴트 멤버
서브 클래스에서 접근할 수 없음 슈퍼 클래스의 디폴트 멤버 서브 클래스가 동일한 패키지에 있을 때, 접근 가능 슈퍼 클래스의 public 멤버 서브 클래스는 항상 접근 가능 슈퍼 클래스의 protected 멤버 같은 패키지 내의 모든 클래스 접근 허용 패키지 여부와 상관없이 서브 클래스는 접근 가능

11 슈퍼 클래스 멤버의 접근 지정자

12 protected 멤버 protected 멤버에 대한 접근 같은 패키지의 모든 클래스에게 허용
상속되는 서브 클래스(같은 패키지든 다른 패키지든 상관 없음)에게 허용

13 서브 클래스/슈퍼 클래스의 생성자 호출과 실행
서브 클래스의 객체가 생성될 때 슈퍼클래스 생성자와 서브 클래스 생성자 모두 실행 호출 순서 서브 클래스의 생성자 먼저 호출, 서브 클래스의 생성자는 실행 전 슈퍼 클래스 생성자 호출 실행 순서 슈퍼 클래스의 생성자가 먼저 실행된 후 서브 클래스의 생성자 실행

14 슈퍼 클래스와 서브 클래스의 생성자 호출 및 실행 관계
생성자A 생성자B 생성자C

15 서브 클래스와 슈퍼 클래스의 생성자 선택 서브 클래스의 객체가 생성될 때 슈퍼 클래스와 서브 클래스
각각 여러 개의 생성자 작성 가능 서브 클래스의 객체가 생성될 때 슈퍼 클래스 생성자 1 개와 서브 클래스 생성자 1개가 실행 서브 클래스의 생성자와 슈퍼 클래스의 생성자가 결정되는 방식 1. 개발자의 명시적 선택 서브 클래스 개발자가 슈퍼 클래스의 생성자 명시적 선택 super() 키워드를 이용하여 선택 2. 컴파일러가 기본생성자 선택 서브 클래스 개발자가 슈퍼 클래스의 생성자를 선택하지 않는 경우 컴파일러가 자동으로 슈퍼 클래스의 기본 생성자 선택

16 컴파일러에 의해 슈퍼 클래스의 기본 생성자가 묵시적 선택(1)
개발자가 서브 클래스의 생성자에 대해 슈퍼 클래스의 생성자를 명시적으로 선택하지 않은 경우 생성자A 생성자B

17 슈퍼 클래스에 기본 생성자가 없어 오류 난 경우 오류 메시지
"Implicit super constructor A() is undefined. Must explicitly invoke another constructor"

18 서브 클래스의 매개 변수를 가진 생성자에 대해서도 슈퍼 클래스의 기본 생성자가 자동 선택
개발자가 서브 클래스의 생성자에 대해 슈퍼 클래스의 생성자를 명시적으로 선택하지 않은 경우 생성자A 매개변수생성자B

19 super()로 슈퍼 클래스의 생성자 명시적 선택
서브 클래스에서 명시적으로 슈퍼 클래스의 생성자 선택 호출 사용 방식 super(parameter); 인자를 이용하여 슈퍼 클래스의 적당한 생성자 호출 반드시 서브 클래스 생성자 코드의 제일 첫 라인에 와야 함

20 super()로 슈퍼 클래스의 생성자를 명시적으로 선택한 사례
매개변수생성자A5 매개변수생성자B5

21 예제 5-2 : super()를 활용한 ColorPoint 작성
super()를 이용하여 ColorPoint 클래스의 생성자에서 서브 클래스 Point의 생성자를 호출하는 예를 보인다. class Point { private int x, y; // 한 점을 구성하는 x, y 좌표 Point() { this.x = this.y = 0; } Point(int x, int y) { this.x = x; this.y = y; void showPoint() { // 점의 좌표 출력 System.out.println("(" + x + "," + y + ")"); class ColorPoint extends Point { private String color; // 점의 색 ColorPoint(int x, int y, String color) { super(x, y); // Point의 생성자 Point(x, y) 호출 this.color = color; void showColorPoint() { // 컬러 점의 좌표 출력 System.out.print(color); showPoint(); // Point 클래스의 showPoint() 호출 public class SuperEx { public static void main(String[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showColorPoint(); } x=5, y=6 blue(5,6) x=5, y=6, color = "blue" 전달

22 업캐스팅 개념

23 업캐스팅 업캐스팅(upcasting) 서브 클래스의 레퍼런스를 슈퍼 클래스 레퍼런스에 대입
슈퍼 클래스 레퍼런스로 서브 클래스 객체를 가리키게 되는 현상 생물이 들어가는 박스에 사람이나 코끼리를 넣어도 무방 * 사람이나 코끼리 모두 생물을 상속받았기 때문 class Person { } class Student extends Person { } Person p; Student s = new Student(); p = s; // 업캐스팅 슈퍼클래스 레퍼런스로 객체 내의 슈퍼 클래스의 멤버만 접근 가능 p.grade = "A"; // grade는 Person의 // 멤버가 아니므로 // 컴파일 오류

24 업캐스팅 사례 class Person { String name; String id;
public Person(String name) { this.name = name; } class Student extends Person { String grade; String department; public Student(String name) { super(name); public class UpcastingEx { public static void main(String[] args) { Person p; Student s = new Student("이재문"); p = s; // 업캐스팅 발생 System.out.println(p.name); // 오류 없음 p.grade = "A"; // 컴파일 오류 p.department = "Com"; // 컴파일 오류 이재문

25 다운캐스팅 다운캐스팅(downcasting) 슈퍼 클래스 레퍼런스를 서브 클래스 레퍼런스에 대입
업캐스팅된 것을 다시 원래대로 되돌리는 것 반드시 명시적 타입 변환 지정 class Person { } class Student extends Person { } Person p = new Student("이재문"); // 업캐스팅 Student s = (Student)p; // 다운캐스팅, 강제타입변환

26 다운캐스팅 사례 public class DowncastingEx {
public static void main(String[] args) { Person p = new Student("이재문"); // 업캐스팅 Student s; s = (Student)p; // 다운캐스팅 System.out.println(s.name); // 오류 없음 s.grade = "A"; // 오류 없음 } 이재문

27 ? 업캐스팅 레퍼런스로 객체 구별? p  업캐스팅된 레퍼런스로는 객체의 실제 타입을 구분하기 어려움
슈퍼 클래스는 여러 서브 클래스에 상속되기 때문 예) 아래의 클래스 계층 구조에서, p가 가리키는 객체가 Person 객체인지, Student 객체인지, Professor 객체인지 구분하기 어려움 Person p = new Person(); Person p = new Student(); // 업캐스팅 Person p = new Professor(); // 업캐스팅 Person 타입의 레퍼런스 p로 업캐스팅 p Person 객체 Student 객체 Professor 객체 ? 샘플 클래스 계층 구조 p가 가리키는 객체가 Person 객체인지, Student 객체인지, Professor 객체인지 구분하기 어려움

28 instanceof 연산자 사용 instanceof 연산자 instanceof 연산자 사용 사례
레퍼런스가 가리키는 객체의 타입 식별 instanceof 연산자 사용 사례 객체레퍼런스 instanceof 클래스타입 연산의 결과 : true/false의 불린 값 new Professor() 객체는 Professor 타입이면서, 동시에 Researcher 타입이기도 하고, Person 타입이기도 함 Person p = new Professor(); if(p instanceof Person) // true if(p instanceof Student) // false. Student를 상속받지 않기 때문 if(p instanceof Researcher) // true if(p instanceof Professor) // true if("java" instanceof String) // true if(3 instanceof int) // 문법 오류. instanceof는 객체에 대한 레퍼런스에만 사용

29 예제 5-3 : instanceof 연산자 활용 class Person { } class Student extends Person { } class Researcher extends Person { } class Professor extends Researcher { } public class InstanceOfEx { static void print(Person p) { if(p instanceof Person) System.out.print("Person "); if(p instanceof Student) System.out.print("Student "); if(p instanceof Researcher) System.out.print("Researcher "); if(p instanceof Professor) System.out.print("Professor "); System.out.println(); } public static void main(String[] args) { System.out.print("new Student() -> "); print(new Student()); System.out.print("new Researcher() -> "); print(new Researcher()); System.out.print("new Professor() -> "); print(new Professor()); instanceof 연산자를 이용하여 [그림 5-15]의 상속 관계에 따라 레퍼런스가 가리키는 객체의 타입을 알아본다. 실행 결과는 무엇인가? new Professor() 객체는 Person 타입이기도 하고 Researcher 타입이기도 하고, Professor 타입이기도 함 new Student() -> Person Student new Researcher() -> Person Researcher new Professor() -> Person Researcher Professor

30 메소드 오버라이딩의 개념 메소드 오버라이딩(Method Overriding) 오버라이딩 조건
서브 클래스에서 슈퍼 클래스의 메소드 중복 작성 슈퍼 클래스의 메소드 무력화, 항상 서브 클래스에 오버라이딩한 메소드가 실행되도록 보장됨 “메소드 무시하기”로 번역되기도 함 오버라이딩 조건 슈퍼 클래스 메소드의 원형(메소드 이름, 인자 타입 및 개수, 리턴 타입) 동일하게 작성

31 서브 클래스 객체와 오버라이딩된 메소드 호출 - 오버라이딩한 메소드가 실행됨을 보장
서브 클래스 객체와 오버라이딩된 메소드 호출 - 오버라이딩한 메소드가 실행됨을 보장 오버라이딩된 메소드, B의 f() 직접 호출 class A { void f() { System.out.println("A의 f() 호출"); } class B extends A { void f() { // 클래스 A의 f()를 오버라이딩 System.out.println("B의 f() 호출"); (b) A의 f()를 호출해도, 오버라이딩된 메소드, B의 f()가 실행됨

32 오버라이딩의 목적, 다형성 실현 오버라이딩으로 다형성 실현 하나의 인터페이스(같은 이름)에 서로 다른 구현
슈퍼 클래스의 메소드를 서브 클래스에서 각각 목적에 맞게 다르게 구현 사례 Shape의 draw() 메소드를 Line, Rect, Circle에서 오버라이딩하여 다르게 구현

33 예제 5-4 : 메소드 오버라이딩으로 다형성 실현 Shape의 draw() 메소드를 Line, Circle, Rect 클래스에서 목적에 맞게 오버라이딩하는 다형성의 사례를 보여준다. class Shape { // 도형의 슈퍼 클래스 public void draw() { System.out.println("Shape"); } class Line extends Shape { public void draw() { // 메소드 오버라이딩 System.out.println("Line"); class Rect extends Shape { System.out.println("Rect"); class Circle extends Shape { System.out.println("Circle"); public class MethodOverridingEx { static void paint(Shape p) { // Shape을 상속받은 객체들이 // 매개 변수로 넘어올 수 있음 p.draw(); // p가 가리키는 객체에 오버라이딩된 draw() 호출. // 동적바인딩 } public static void main(String[] args) { Line line = new Line(); paint(line); // Line의 draw() 실행. "Line" 출력 paint(new Shape()); // Shape의 draw() 실행. "Shape" 출력 paint(new Line()); // 오버라이딩된 메소드 Line의 draw() 실행 paint(new Rect()); // 오버라이딩된 메소드 Rect의 draw() 실행 paint(new Circle()); // 오버라이딩된 메소드 Circle의 draw() 실행 동적바인딩 Line Shape Rect Circle

34 예제 5-4 실행 과정

35 동적 바인딩 – 오버라이딩된 메소드 호출 * 오버라이딩 메소드가 항상 호출된다. class SuperObject {
protected String name; public void paint() { draw(); } public void draw() { System.out.println("Super Object"); public class SubObject extends SuperObject { System.out.println("Sub Object"); public static void main(String [] args) { SuperObject b = new SubObject(); b.paint(); 동적바인딩 public class SuperObject { protected String name; public void paint() { draw(); } public void draw() { System.out.println("Super Object"); public static void main(String [] args) { SuperObject a = new SuperObject(); a.paint(); Super Object Sub Object

36 super 키워드로 슈퍼 클래스의 멤버 접근 super 슈퍼 클래스의 멤버를 접근할 때 사용되는 레퍼 런스
서브 클래스에서만 사용 슈퍼 클래스의 필드 접근 슈퍼 클래스의 메소드 호출 시 super로 이루어지는 메소드 호출 : 정적 바인딩 동적 바인딩 class SuperObject { protected String name; public void paint() { draw(); } public void draw() { System.out.println(name); public class SubObject extends SuperObject { name = "Sub"; super.name = "Super"; super.draw(); public static void main(String [] args) { SuperObject b = new SubObject(); b.paint(); 정적 바인딩 Super Sub

37 오버로딩과 오버라이딩

38 추상 클래스 추상 메소드(abstract method) 추상 클래스(abstract class)
public abstract String getName(); // 추상 메소드 public abstract String fail() { return "Good Bye"; } // 추상 메소드 아님. 컴파일 오류 // 추상 메소드를 가진 추상 클래스 abstract class Shape { Shape() { ... } void edit() { ... } abstract public void draw(); // 추상 메소드 } // 추상 메소드 없는 추상 클래스 abstract class JComponent { String name; void load(String name ) { this.name= name; } class fault { // 오류. 추상 메소드를 가지고 있으므로 abstract로 선언되어야 함 abstract void f(); // 추상 메소드 }

39 추상 클래스의 인스턴스 생성 불가 추상 클래스는 온전한 클래스가 아니기 때문에 인스턴 스를 생성할 수 없음
JComponent p; // 오류 없음. 추상 클래스의 레퍼런스 선언 p = new JComponent(); // 컴파일 오류. 추상 클래스의 인스턴스 생성 불가 Shape obj = new Shape(); // 컴파일 오류. 추상 클래스의 인스턴스 생성 불가 컴파일 오류 메시지 Unresolved compilation problem: Cannot instantiate the type Shape

40 추상 클래스의 상속과 구현 추상 클래스 상속 추상 클래스 구현 추상 클래스를 상속받으면 추상 클래스가 됨
서브 클래스도 abstract로 선언해야 함 추상 클래스 구현 서브 클래스에서 슈퍼 클래스의 추상 메소드 구현(오버라이딩) 추상 클래스를 구현한 서브 클래스는 추상 클래스 아님 abstract class A { // 추상 클래스 abstract int add(int x, int y); // 추상 메소드 } abstract class B extends A { // 추상 클래스 void show() { System.out.println("B"); } A a = new A(); // 컴파일 오류. 추상 클래스의 인스턴스 생성 불가 B b = new B(); // 컴파일 오류. 추상 클래스의 인스턴스 생성 불가 class C extends A { // 추상 클래스 구현. C는 정상 클래스 int add(int x, int y) { return x+y; } // 추상 메소드 구현. 오버라이딩 void show() { System.out.println("C"); } } ... C c = new C(); // 정상

41 추상 클래스의 목적 추상 클래스의 목적 상속을 위한 슈퍼 클래스로 활용하는 것 서브 클래스에서 추상 메소드 구현 다형성 실현
class Shape { public void draw() { System.out.println("Shape"); } 추상 클래스의 목적 상속을 위한 슈퍼 클래스로 활용하는 것 서브 클래스에서 추상 메소드 구현 다형성 실현 abstract class Shape { public abstract void draw(); } 추상 클래스로 작성 추상 클래스를 상속받아 추상 메소드 draw() 구현 class Line extends DObject { public void draw() { System.out.println("Line"); } class Rect extends DObject { public void draw() { System.out.println("Rect"); } class Circle extends DObject { public void draw() { System.out.println("Circle"); }

42 예제 5-5 : 추상 클래스의 구현 추상 클래스 Calculator를 상속받는 GoodCalc 클래스를 구현하라.
abstract class Calculator { public abstract int add(int a, int b); public abstract int subtract(int a, int b); public abstract double average(int[] a); }

43 예제 5-5 정답 public class GoodCalc extends Calculator {
public int add(int a, int b) { // 추상 메소드 구현 return a + b; } public int subtract(int a, int b) { // 추상 메소드 구현 return a - b; public double average(int[] a) { // 추상 메소드 구현 double sum = 0; for (int i = 0; i < a.length; i++) sum += a[i]; return sum/a.length; public static void main(String [] args) { GoodCalc c = new GoodCalc(); System.out.println(c.add(2,3)); System.out.println(c.subtract(2,3)); System.out.println(c.average(new int [] { 2,3,4 })); 5 -1 3.0

44 인터페이스의 필요성 정해진 규격(인터페이스)에 맞기만 하면 연결 가능. 정해진 규격(인터페이스)에 맞지 않으면 연결 불가
각 회사마다 구현 방법은 다름

45 자바 인터페이스 자바 인터페이스 자바 인터페이스의 특징 상수와 추상 메소드로만 구성 : 변수 필드 없음 인터페이스 선언
interface 키워드로 선언 자바 인터페이스의 특징 상수와 추상 메소드로만 구성 메소드 : public abstract 타입으로 생략 가능 상수 : public static final 타입으로 생략 가능 인터페이스의 객체 생성 불가 interface PhoneInterface { int BUTTONS = 20; // 상수 필드 선언 void sendCall(); // 추상 메소드 void receiveCall(); // 추상 메소드 } public interface로서 public 생략 가능 public static final로서 public static final 생략 가능 abstract public 으로서 abstract public 생략 가능 new PhoneInterface(); // 오류. 인터페이스의 객체를 생성할 수 없다.

46 인터페이스 상속 인터페이스 간에 상속 가능 인터페이스 다중 상속 허용 인터페이스를 상속하여 확장된 인터페이스 작성 가능
extends 키워드로 상속 선언 예) 인터페이스 다중 상속 허용 interface MobilePhoneInterface extends PhoneInterface { void sendSMS(); // 새로운 추상 메소드 추가 void receiveSMS(); // 새로운 추상 메소드 추가 } interface MusicPhoneInterface extends PhoneInterface, MP3Interface { ...... }

47 인터페이스 구현 인터페이스 구현 인터페이스를 상속받아, 모든 추상 메소드를 구현한 클래스 선언
implements 키워드로 인터페이스 구현 예) 여러 개의 인터페이스 동시 구현도 가능 클래스 상속과 인터페이스 구현을 동시에 할 수 있음 예제 5-6 class FeaturePhone implements MobilePhoneInterface { // 인터페이스 구현 public void sendCall() { ... } public void receiveCall() { ... } public void sendSMS() { ... } public void receiveSMS() { ... } // 다른 메소드 추가 가능 public int getButtons() { ... } } MobilePhoneInterface의 모든 메소드 구현

48 예제 5-6 : 인터페이스 구현과 동시에 슈퍼 클래스 상속
interface PhoneInterface { int BUTTONS = 20; void sendCall(); void receiveCall(); } interface MobilePhoneInterface extends PhoneInterface { void sendSMS(); void receiveSMS(); interface MP3Interface { public void play(); public void stop(); class PDA { public int calculate(int x, int y) { return x + y; // SmartPhone 클래스는 PDA를 상속받고, // MobilePhoneInterface와 MP3Interface 인터페이스에 선언된 // 메소드를 모두 구현 class SmartPhone extends PDA implements MobilePhoneInterface, MP3Interface { public void sendCall() { System.out.println("전화 걸기"); } public void receiveCall() { System.out.println("전화 받기"); } public void sendSMS() { System.out.println("SMS 보내기"); } public void receiveSMS() { System.out.println("SMS 받기"); } public void play() { System.out.println("음악 재생"); } public void stop() { System.out.println("재생 중지"); } public void schedule() { System.out.println("일정 관리"); } } public class InterfaceEx { public static void main(String [] args) { SmartPhone p = new SmartPhone(); p.sendCall(); p.play(); System.out.println(p.calculate(3,5)); p.schedule(); MobilePhoneInterface 모든 메소드 구현 MP3Interface의 모든 메소드 구현 새로운 메소드 추가 전화 걸기 음악 재생 8 일정 관리


Download ppt "명품 JAVA Essential."

Similar presentations


Ads by Google