class Person { String name; int age; String introduce() { } }
Person & Killer
class Killer extends Person { String warning; String weapon; String getWeapon() { } }
일반화
특수화
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()
name = 요츠바 age = 5
warning = You..
weapon = 총
Person
Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");
yotsuba
Killer
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() { ... }
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()
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()
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 + "세"; } }
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 + "세"; } }
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
Person
Killer
Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");
Parent <= Child
Person
Killer
Parent <= Child
UpCasting
Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");
Person
Killer
Child <= Parent
Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총"); Killer killerYotsuba = yotsuba;
Person
Killer
Child <= Parent
Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총"); Killer killerYotsuba = yotsuba;
Compile Error!
Person
Killer
Child <= Parent
DownCasting
Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총"); Killer killerYotsuba = yotsuba;
Person
Killer
Child <= (Child)Parent
DownCasting
Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총"); Killer killerYotsuba = (Killer)yotsuba;
Compile Time Person yotsuba = ... Killer killeryotsuba = ...
Person yotsuba = killeryotsuba; Killer killeryotsuba = (Killer)yotsuba;
실제 생성된 객체 타입이 무엇이건 상관없이
참조 변수의 타입만을 이용해 업/다운 캐스팅
Runtime Person yotsuba = new Person("요츠바", 5);
Killer killeryotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");
Killer killeryotsuba = (Killer)yotsuba;
실제 생성된 객체 타입을 이용해
업/다운 캐스팅
ClassCastException
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(); }
Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총"); yotsuba.introduce();
무기 : 총, 이름 : 요츠바, 나이 5세
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);
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 + "세"; }
void method(Person person) { ... }
이것도 가능합니다
method(new Killer("요츠바", 5, "You can tell me in hell.", "총"));
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.", "총"));
public class IronMan { Person person; IronMan(Person person) { this.person = person; } String introduce() { return person.introduce(); } }
Polymorphism
public class IronMan { Person person; IronMan(Person person) { this.person = person; } String introduce() { return person.introduce(); } }
IronMan.introduce()
Person Role
class Person { } class Killer extends Person { } class Musician extends Person { }
Person
Killer Musician
UpCasting
class Person { } class Killer extends Person { } class Musician extends Person { }
Person
Killer Musician
class Person { String name; int age; abstract String introduce(); }
Abstract Method
Child Class는 반드시 Abstract Method를 Override 해야 합니다
abstract class Person { String name; int age; abstract String introduce(); }
Abstract Class
Abstract Method를 가진 Class는
Abstract Class로 선언되어야 합니다.
abstract class Person { String name; int age; abstract String introduce(); }
Abstract Class
Abstract Class는 new를 이용해 인스턴스를 생성할 수 없습니다.
class Kid extends Person { String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; } }
Kid class
class Killer extends Person {
String introduce() { return "무기 : " + weapon + ", " + "이름 : " + name + ", 나이 " + age + "세"; } }
Killer class
class Musician extends Person {
String introduce() { return "악기 : " + instrument + ", " + "이름 : " + name + ", 나이 " + age + "세"; } }
Musician class
abstract class Person { String name; int age; abstract String introduce(); }
Specification
IronMan과 협력하려면 introduce를 구현해야 한다
abstract class Person { String name; int age; abstract String introduce(); }
Remove Code Duplication
Person 역할을 하는 모든 객체들은 name과 age를 가져야 한다
public class Kid extends Person { public String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; } }
인터페이스에서 받은 메소드는 public으로
public class IronMan { Tellable tellable; IronMan(Tellable tellable) { this.tellable = tellable; } String introduce() { return tellable.introduce(); } }
IronMan – Tellable Interface
http://www.slideshare.net/baejja
e93/dependency-injection-
36867592
Coupling & Dependency