14. 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() { ... }
16. 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()
17. 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()
18. 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 + "세";
}
}
19. 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 + "세";
}
}
20. name = 요츠바
age = 5
warning = You..
weapon = 총
this
class Person
String introduce() {
return "이름 : " + name +
", 나이 " + age + "세";
}
class
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
21. To p i c s
UpCasting
Polymorphism
Polymorphic Arguments
Role
Abstract Class
Interface
26. Person
Killer
Child <= Parent
Person yotsuba = new Killer("요츠바", 5,
"You can tell me in hell.", "총");
Killer killerYotsuba = yotsuba;
Compile Error!
32. Compile Time
Person yotsuba = ...
Killer killeryotsuba = ...
Person yotsuba = killeryotsuba;
Killer killeryotsuba = (Killer)yotsuba;
실제 생성된 객체 타입이 무엇이건 상관없이
참조 변수의 타입만을 이용해 업/다운 캐스팅
33. Runtime
Person yotsuba = new Person("요츠바", 5);
Killer killeryotsuba = new Killer("요츠바", 5,
"You can tell me in hell.", "총");
Killer killeryotsuba = (Killer)yotsuba;
실제 생성된 객체 타입을 이용해
업/다운 캐스팅
ClassCastException
34. 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();
}
35. To p i c s
UpCasting
Polymorphism
Polymorphic Arguments
Role
Abstract Class
Interface
43. 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);
44. 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 + "세";
}
50. 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.", "총"));
51. public class IronMan {
Person person;
IronMan(Person person) {
this.person = person;
}
String introduce() {
return person.introduce();
}
}
Polymorphism
52. To p i c s
UpCasting
Polymorphism
Polymorphic Arguments
Role
Abstract Class
Interface