Top Banner
PL in Java Week04 Polymorphism 조영호 [email protected]
101

[NHN NEXT] Java 강의 - Week4

Mar 15, 2018

Download

Software

YoungHo Cho
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: [NHN NEXT] Java 강의 - Week4

PL in Java Week04

Polymorphism

조영호 [email protected]

Page 2: [NHN NEXT] Java 강의 - Week4

지난 주에…

Page 3: [NHN NEXT] Java 강의 - Week4

사람과 킬러

Page 4: [NHN NEXT] Java 강의 - Week4

일반화와 특수화 일반화 특수화

Page 5: [NHN NEXT] Java 강의 - Week4

일반화와 특수화 일반화 특수화

설명은 더 추상적이고

집합의 크기는 더 크고

Page 6: [NHN NEXT] Java 강의 - Week4

일반화와 특수화 일반화 특수화

설명은 더 구체적이고

집합의 크기는 더 작고

Page 7: [NHN NEXT] Java 강의 - Week4

일반화와 특수화 일반화 특수화

특수화 집합은 일반화 집합의 부분집합

Page 8: [NHN NEXT] Java 강의 - Week4

사람 킬러 더 일반적인 개념

더 특수한 개념

Page 9: [NHN NEXT] Java 강의 - Week4

class Person { String name; int age; String introduce() { } }

Person & Killer

class Killer extends Person { String warning; String weapon; String getWeapon() { } }

일반화

특수화

Page 10: [NHN NEXT] Java 강의 - Week4

is-a Relationship

Killer is-a Person

Page 11: [NHN NEXT] Java 강의 - Week4

Person Killer Class

Class

new Killer()

new Killer()

new Killer()

new Person()

new Person()

new Person()

new Person()

new Person()

new Person()

new Person()

new Person()

new Person()

new Person()

Page 12: [NHN NEXT] Java 강의 - Week4

name = 요츠바 age = 5

warning = You..

weapon = 총

Person

Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");

yotsuba

Killer

Page 13: [NHN NEXT] Java 강의 - Week4

this

Page 14: [NHN NEXT] Java 강의 - Week4

class Person { String name; int age; ... String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; } }

Person yotsuba = new Person("요츠바", 5);

name = 요츠바 age = 5

this

class Person String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; }

class

class Object

superclass

String toString() { ... }

Page 15: [NHN NEXT] Java 강의 - Week4

Method Lookup

this에서 시작

Page 16: [NHN NEXT] Java 강의 - Week4

class Person { String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; } }

name = 요츠바 age = 5

this

class Person String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; }

class

class Object

superclass

String toString() { ... }

yotsuba.introduce()

Page 17: [NHN NEXT] Java 강의 - Week4

class Person { String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; } }

name = 요츠바 age = 5

this

class Person String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; }

class

class Object

superclass

String toString() { ... }

yotsuba.toString()

Page 18: [NHN NEXT] Java 강의 - Week4

class Killer extends Person { }

Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");

name = 요츠바 age = 5

warning = You..

weapon = 총

this

class Person String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; }

class

class Object

superclass

String toString() { ... }

class Killer

superclass

class Person { String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; } }

Page 19: [NHN NEXT] Java 강의 - Week4

name = 요츠바 age = 5

warning = You..

weapon = 총

this

class Person String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; }

class

class Object

superclass

String toString() { ... }

class Killer

superclass

yotsuba.introduce()

class Killer extends Person { }

class Object

class Person { String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; } }

Page 20: [NHN NEXT] Java 강의 - Week4

name = 요츠바 age = 5

warning = You..

weapon = 총

this

class Person String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; }

class

class Object

superclass

String toString() { ... }

class Killer

superclass

yotsuba.introduce()

String introduce() { return "무기 : " + weapon + "," + "이름 : " + name + ", 나이 " + age + "세"; }

class Killer extends Person { String introduce() { return "무기 : " + weapon + "," + "이름 : " + name + ", 나이 " + age + "세"; } }

class Person { String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; } }

class Object

Page 21: [NHN NEXT] Java 강의 - Week4

To p i c s

UpCasting

Polymorphism

Polymorphic Arguments

Role

Abstract Class

Interface

Page 22: [NHN NEXT] Java 강의 - Week4

class Person { } class Killer extends Person { }

Person

Killer

UML

Page 23: [NHN NEXT] Java 강의 - Week4

Person

Killer

Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");

Parent <= Child

Page 24: [NHN NEXT] Java 강의 - Week4

Person

Killer

Parent <= Child

UpCasting

Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");

Page 25: [NHN NEXT] Java 강의 - Week4

Person

Killer

Child <= Parent

Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총"); Killer killerYotsuba = yotsuba;

Page 26: [NHN NEXT] Java 강의 - Week4

Person

Killer

Child <= Parent

Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총"); Killer killerYotsuba = yotsuba;

Compile Error!

Page 27: [NHN NEXT] Java 강의 - Week4

Person

Killer

Child <= Parent

DownCasting

Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총"); Killer killerYotsuba = yotsuba;

Page 28: [NHN NEXT] Java 강의 - Week4

Person

Killer

Child <= (Child)Parent

DownCasting

Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총"); Killer killerYotsuba = (Killer)yotsuba;

Page 29: [NHN NEXT] Java 강의 - Week4

Person

Killer

Killer yotsuba = (Killer)new Person("요츠바", 5);

Compile Success

Page 30: [NHN NEXT] Java 강의 - Week4

Person

Killer

Killer yotsuba = (Killer)new Person("요츠바", 5);

Runtime Exception

ClassCastException

Page 31: [NHN NEXT] Java 강의 - Week4

Compile Time

Runtime

vs

Page 32: [NHN NEXT] Java 강의 - Week4

Compile Time Person yotsuba = ... Killer killeryotsuba = ...

Person yotsuba = killeryotsuba; Killer killeryotsuba = (Killer)yotsuba;

실제 생성된 객체 타입이 무엇이건 상관없이

참조 변수의 타입만을 이용해 업/다운 캐스팅

Page 33: [NHN NEXT] Java 강의 - Week4

Runtime Person yotsuba = new Person("요츠바", 5);

Killer killeryotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");

Killer killeryotsuba = (Killer)yotsuba;

실제 생성된 객체 타입을 이용해

업/다운 캐스팅

ClassCastException

Page 34: [NHN NEXT] Java 강의 - Week4

Runtime - instanceof

instanceof DownCast

Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총"); if (yotsuba instanceof Killer) { Killer killerYotsuba = (Killer)yotsuba; killerYotsuba.getWeapon(); }

Page 35: [NHN NEXT] Java 강의 - Week4

To p i c s

UpCasting

Polymorphism

Polymorphic Arguments

Role

Abstract Class

Interface

Page 36: [NHN NEXT] Java 강의 - Week4

Polymorphism

many forms

Page 37: [NHN NEXT] Java 강의 - Week4

서로 다른 타입

서로 다른 행동

같은 메시지

Page 38: [NHN NEXT] Java 강의 - Week4

Person yotsuba = ... yotsuba.introduce();

Page 39: [NHN NEXT] Java 강의 - Week4

Person yotsuba = new Person("요츠바", 5); yotsuba.introduce();

이름 : 요츠바, 나이 5세

Page 40: [NHN NEXT] Java 강의 - Week4

Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총"); yotsuba.introduce();

무기 : 총, 이름 : 요츠바, 나이 5세

Page 41: [NHN NEXT] Java 강의 - Week4

UpCasting

Person = Person

Person = Killer

Page 42: [NHN NEXT] Java 강의 - Week4

this

Method Lookup

Page 43: [NHN NEXT] Java 강의 - Week4

class Person { String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; } }

name = 요츠바 age = 5

this

class Person String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; }

class

class Object

superclass

String toString() { ... }

yotsuba.introduce()

Person yotsuba = new Person("요츠바", 5);

Page 44: [NHN NEXT] Java 강의 - Week4

name = 요츠바 age = 5

warning = You..

weapon = 총

this

class Person String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; }

class

class Object

superclass

String toString() { ... }

class Killer

superclass yotsuba.introduce()

class Killer extends Person { String introduce() { return "무기 : " + weapon + "," + "이름 : " + name + ", 나이 " + age + "세"; } }

class Person { String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; } }

Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");

String introduce() { return "무기 : " + weapon + "," + "이름 : " + name + ", 나이 " + age + "세"; }

Page 45: [NHN NEXT] Java 강의 - Week4

Runtime Binding

or

Dynamic Binding

Page 46: [NHN NEXT] Java 강의 - Week4

To p i c s

UpCasting

Polymorphism

Polymorphic Arguments

Role

Abstract Class

Interface

Page 47: [NHN NEXT] Java 강의 - Week4

Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");

이게 가능하다면

Page 48: [NHN NEXT] Java 강의 - Week4

void method(Person person) { ... }

이것도 가능합니다

method(new Killer("요츠바", 5, "You can tell me in hell.", "총"));

Page 49: [NHN NEXT] Java 강의 - Week4

IronMan Person

Page 50: [NHN NEXT] Java 강의 - Week4

class IronMan { Person person; IronMan(Person person) { this.person = person; } }

생성자도 일종의 메소드

IronMan ironMan = new IronMan(new Person("토니 스타크", 40)); IronMan ironYotsuba = new IronMan(new Killer("요츠바", 5, "You can tell me in hell.", "총"));

Page 51: [NHN NEXT] Java 강의 - Week4

public class IronMan { Person person; IronMan(Person person) { this.person = person; } String introduce() { return person.introduce(); } }

Polymorphism

Page 52: [NHN NEXT] Java 강의 - Week4

To p i c s

UpCasting

Polymorphism

Polymorphic Arguments

Role

Abstract Class

Interface

Page 53: [NHN NEXT] Java 강의 - Week4

역할, 책임, 협력

Page 54: [NHN NEXT] Java 강의 - Week4

시체를 검시해 주게

사건을 수사해 주세요

경감

탐정 조수

역할

책임

협력

Page 55: [NHN NEXT] Java 강의 - Week4

public class IronMan { Person person; IronMan(Person person) { this.person = person; } String introduce() { return person.introduce(); } }

IronMan.introduce()

Page 56: [NHN NEXT] Java 강의 - Week4

introduce introduce

Collaboration

IronMan Person

Page 57: [NHN NEXT] Java 강의 - Week4

introduce introduce

Responsibility

IronMan Person

Page 58: [NHN NEXT] Java 강의 - Week4

IronMan Person introduce introduce

Role

Page 59: [NHN NEXT] Java 강의 - Week4

IronMan Person introduce introduce

Introduce라는 메시지를 수신하고

처리할 수만 있다면

introduce

Page 60: [NHN NEXT] Java 강의 - Week4

IronMan introduce introduce

모두 다 Person 역할을 수행할 수 있다고

볼 수 있습니다.

Person

Page 61: [NHN NEXT] Java 강의 - Week4

IronMan introduce introduce

다형성은 동일한 역할을 수행하는 객체들이

동일한 메시지에 대해

서로 다른 방식으로 반응하는 것을 의미

Person

Page 62: [NHN NEXT] Java 강의 - Week4
Page 63: [NHN NEXT] Java 강의 - Week4

IronMan introduce introduce

Person

in OO Paradigm

Generalization

Page 64: [NHN NEXT] Java 강의 - Week4

IronMan introduce introduce

Person

in Class-Based OOPL

Inheritance

Page 65: [NHN NEXT] Java 강의 - Week4

Person Role

class Person { } class Killer extends Person { } class Musician extends Person { }

Person

Killer Musician

Page 66: [NHN NEXT] Java 강의 - Week4

IronMan introduce introduce

모두 다 Person 역할을 수행할 수 있다는 것은

Person

Page 67: [NHN NEXT] Java 강의 - Week4

IronMan introduce introduce

Person 대신 사용될 수 있다는 것을 의미합니다

Person

Page 68: [NHN NEXT] Java 강의 - Week4

UpCasting

class Person { } class Killer extends Person { } class Musician extends Person { }

Person

Killer Musician

Page 69: [NHN NEXT] Java 강의 - Week4

IronMan은 미래에 등장할 Person의

서브 클래스와도 협력이 가능

Person

Killer Musician

IronMan

Page 70: [NHN NEXT] Java 강의 - Week4

Flexible

Page 71: [NHN NEXT] Java 강의 - Week4

To p i c s

UpCasting

Polymorphism

Polymorphic Arguments

Role

Abstract Class

Interface

Page 72: [NHN NEXT] Java 강의 - Week4

IronMan introduce introduce

Person

IronMan의 입장에서는

Page 73: [NHN NEXT] Java 강의 - Week4

IronMan introduce introduce

Person 대신 사용되는 객체들이 introduce

메시지를 이해할 수만 있으면 됩니다.

Person

Page 74: [NHN NEXT] Java 강의 - Week4

IronMan introduce introduce

Introduce에 대한 메소드가 실제로 어떻게

구현되어 있는지는 상관이 없습니다.

Person

Page 75: [NHN NEXT] Java 강의 - Week4

class Person { String name; int age; String introduce() { } }

introduce()의 시그니처만 알면 되죠

Page 76: [NHN NEXT] Java 강의 - Week4

class Person { String name; int age; abstract String introduce(); }

Abstract Method

Child Class는 반드시 Abstract Method를 Override 해야 합니다

Page 77: [NHN NEXT] Java 강의 - Week4

abstract class Person { String name; int age; abstract String introduce(); }

Abstract Class

Abstract Method를 가진 Class는

Abstract Class로 선언되어야 합니다.

Page 78: [NHN NEXT] Java 강의 - Week4

abstract class Person { String name; int age; abstract String introduce(); }

Abstract Class

Abstract Class는 new를 이용해 인스턴스를 생성할 수 없습니다.

Page 79: [NHN NEXT] Java 강의 - Week4

class Kid extends Person { String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; } }

Kid class

Page 80: [NHN NEXT] Java 강의 - Week4

class Killer extends Person {

String introduce() { return "무기 : " + weapon + ", " + "이름 : " + name + ", 나이 " + age + "세"; } }

Killer class

Page 81: [NHN NEXT] Java 강의 - Week4

class Musician extends Person {

String introduce() { return "악기 : " + instrument + ", " + "이름 : " + name + ", 나이 " + age + "세"; } }

Musician class

Page 82: [NHN NEXT] Java 강의 - Week4

abstract class Person { String name; int age; abstract String introduce(); }

Specification

IronMan과 협력하려면 introduce를 구현해야 한다

Page 83: [NHN NEXT] Java 강의 - Week4

abstract class Person { String name; int age; abstract String introduce(); }

Remove Code Duplication

Person 역할을 하는 모든 객체들은 name과 age를 가져야 한다

Page 84: [NHN NEXT] Java 강의 - Week4

To p i c s

UpCasting

Polymorphism

Polymorphic Arguments

Role

Abstract Class

Interface

Page 85: [NHN NEXT] Java 강의 - Week4

IronMan introduce introduce

Person

Iron과 함께 협력하는 클래스들은

Page 86: [NHN NEXT] Java 강의 - Week4

IronMan introduce introduce

Person

IronMan과 함께 협력하는 클래스들은

Page 87: [NHN NEXT] Java 강의 - Week4

Person의 서브 클래스들로 한정

Person

Killer Musician

IronMan

Page 88: [NHN NEXT] Java 강의 - Week4

만약 IronMan 안에 펭귄을 넣고 싶다면?

Page 89: [NHN NEXT] Java 강의 - Week4

Person 상속 계층에 속하지 않은 객체와도

협력해야 합니다.

Person

Killer Musician

IronMan

Penguin

Animal

Page 90: [NHN NEXT] Java 강의 - Week4

class Person { } class Killer extends Person { }

Person

Killer

Java - Single Inheritance

Page 91: [NHN NEXT] Java 강의 - Week4

Multiple Inheritance

Person

Killer Musician

IronMan

Penguin

Animal

Page 92: [NHN NEXT] Java 강의 - Week4

Animal is-a Person???

Person

Killer Musician

IronMan

Penguin

Animal

Page 93: [NHN NEXT] Java 강의 - Week4

Interface

Page 94: [NHN NEXT] Java 강의 - Week4

interface Tellable { String introduce(); }

메소드 구현이 없습니다

모든 메소드가 abstract method

Page 95: [NHN NEXT] Java 강의 - Week4

public abstract class Person implements Tellable { ... }

Person implements Tellable

Page 96: [NHN NEXT] Java 강의 - Week4

public class Kid extends Person { public String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; } }

인터페이스에서 받은 메소드는 public으로

Page 97: [NHN NEXT] Java 강의 - Week4

public abstract class Animal implements Tellable { ... }

Animal implements Tellable

Page 98: [NHN NEXT] Java 강의 - Week4

public class IronMan { Tellable tellable; IronMan(Tellable tellable) { this.tellable = tellable; } String introduce() { return tellable.introduce(); } }

IronMan – Tellable Interface

Page 99: [NHN NEXT] Java 강의 - Week4

Person

Killer Musician

IronMan

Penguin

Animal

<<interface>>

Tellable

Page 100: [NHN NEXT] Java 강의 - Week4

More Flexible

클래스 계층 구조에 독립적