-
Polymorphism, Upcasting, And DowncastingModeling/TheoremParadigm 2019. 8. 18. 20:21
1. Overview
Organize Polymorphism, Upcasting, and Downcasting. and then find usages and pitfalls each casting.
2. Description
2.1 Polymorphism
The ability of an object to take on many forms. All methods in Java are virtual by default. so Any method can be overridden when used in inheritance unless that method is declared as final or static. When we cast object only the reference type of the object is changed but not the actual object type.
2.2 Upcasting
Derived class cast to Base class. Upcasting is safe and it does not fail.
Mammal m = new Cat();
2.2.1 pitfalls
- Object Slicing
- Losing where it is derived from.
2.3 Downcasting
Base class cast to a derived class. We need to check the instance of the object when we downcast the object using instanceof operator or we might get ClassCastException.
Cat c = (Cat) new Animal();
2.3.1 pitfalls
- Creating a phantom member.
4. Practices
https://github.com/demyank88/DesignPattern/tree/master/src/main/java/kr/co/practice/polymorphism
'Modeling > TheoremParadigm' 카테고리의 다른 글
Locking Strategies and Deadlocks (0) 2020.02.27 Database Scaling (0) 2020.02.23 Cooperative vs Preemptive Multitasking (0) 2019.11.10 Eventual consistency and strict consistency (0) 2019.09.28 Difference between Concurrency and Parallelism (0) 2019.08.18