Polymorphism
- ⭐️
extends
- ⭐️ method overriding
✅ Polymorphism: parent can become child
parent class
child class extends parent class
- 1️⃣ Polymorphism: parent CAN become child instance
- child CAN NOT become parent instance
1
2
3
4
5
Parent parent = new Parent();
Parent parentChild = new Child(); //🟢 parent CAN become child instance = Polymorphism
Parent parentGrandson = new Grandson();
//Child child = new Parent(); //🔴 child CAN NOT become parent instance
- 2️⃣ can only call its type method
- creating
new Child()
instance will create bothchild instance
andparent instance
(aschild
extendsparent
) - however as
parentChild
isParent
type, can only callParent
field and method
1
2
3
Parent parentChild = new Child();
parentChild.parentMethod(); //parentChild is Parent type, can call parent method
//parentChild.childMethod(); // parentChild is Parent type, CAN NOT call child method
new Child()
: 자식 클래스를 생성하면child class
➕parent class
둘 다 생성됨- 그러나
Parent parentChild
이므로parentChild
는Parent
타입이다 - 따라서
parentChild
는Parent
의field, method
만 사용 가능하다
✅ Downcasting
- I am
parent type
inchild instance
, 👎🏻 so I can only call parentfield and methods
🤨 But I want to call child
field and methods
- ➡️ Downcasting: change
parent type
tochild type
- CAN NOT omit
type
1
2
3
4
5
6
7
8
9
10
11
public class CastingMain1 {
public static void main(String[] args) {
Parent parentPoly = new Child(); // parentPoly is Parent type, but Child instance
//parentPoly.childMethod(); // 🔴 compile error, 👎🏻 only can call type method
Child childPoly = (Child) parentPoly; //💡 Downcasting //change parent type to child type
childPoly.childMethod(); //now can call childMethod();
//parentPoly type: parent
//childPoly type: child
}
}
- 🤨 why is downcasting possible?
parent type
is createdchild instance
,- in memory, both
parent instance
andchild instance
is created - thus, can change type from
parent type
tochild type
1
2
3
4
5
6
7
8
9
ublic class CastingMain3 {
public static void main(String[] args) {
Parent parent1 = new Child();
Child child1 = (Child) parent1; //🟢downcasting
Parent parent2 = new Parent();
Child child2 = (Child) parent2; //🔴error, parent2 is parent instance thus downcasting not possible
}
}
Parent parent1 = new Child();
를 생성하면 type은parent
이지만instance
가child
이므로parent instance
,child instance
가 모두 생성된다- 따라서 downcasting 가능 ⭕️
- 그러나
Parent parent2 = new Parent();
하면instance
가parent
이므로parent instance
만 생성 - 따라서 downcasting 불가능 ❌
✅ Upcasting
- Upcasting: changing
child type
toparent type
- can omit
type
1
2
3
4
5
6
7
8
9
public class CastingMain2 {
public static void main(String[] args) {
Child child = new Child();
Parent parent1 = (Parent) child; //upcasting
Parent parent2 = child; //can omit (Parent)
parent1.parentMethod();
}
}
Downcasting 🆚 Upcasting
- Downcasting: 부모 ➡️ 자식
Upcasting: 자식 ➡️ 부모
- Upcasting은 자식 instance를 생성하므로 부모 instance도 모두 항상 생성됨, upcasting하는
type
을 omit하고 사용 가능 - Downcasting은 부모 instance만 생성되므로 downcasting하는
type
을 명시해주어야 한다
✅ InstanceOf
- what is my instance?
- can I downcast to this
instance
? checkinstance
1
2
3
4
5
6
7
8
9
10
11
12
public class CastingMain5 {
public static void main(String[] args) {
Parent parentParent = new Parent();
parentParent instanceOf Parent //true
parentParent instanceOf Child //false, downcast NOT possible
Parent parentChild = new Child();
parentChild instanceOf Parent //true
parentChild instanceOf Child //true, downcast possible
}
}
✅ Polymorphism and overriding
- method: overrided method always has priority
- field: field is not overrided
1
2
3
Parent poly = new Child();
poly.value //field: parent field, field is NOT override
poly.method(); //method: child method, method IS override
poly
type isParent
, instance isChild
- when field is called, go to
Parent
and find, returnParent field
- when method is called, look for override
- if there is override in child, return
Child method
☑️ 결론
- 1️⃣ polymorphism: 부모 type을 자식 instance로 생성할 수 있다(부모는 자식을 품을 수 있음)
- 2️⃣ 자식 instance로 생성하면 자식, 부모 instance를 모두 생성한다
Animal animal = new Dog();
하면animal
,dog
인스턴스 모두 생성됨
- 3️⃣ Downcasting: 부모 ➡️ 자식으로 타입을 바꿈, 내가 명시한 instance일때만 그 type으로 downcast 가능
- 4️⃣ Upcasting: 자식 ➡️ 부모로 타입을 바꿈, 항상 가능
- 5️⃣ 내가 무슨 instance인지 확인하기 위해
instanceOf
를 사용할 수 있다 - 6️⃣ overriding한 method가 항상 우선순위를 가진다(자식 인스턴스)
- method is override, field is not overrided
This post is licensed under CC BY 4.0 by the author.