티스토리 뷰




Upcasting


Upcasting is casting from a subclass to superclass. 

When a superclass variable contains a reference to a subclass object, and that reference is used to call a method, the subclass version of the method is called.


Student s = new Student ();      

Person p = (person)s;

 ↓ 

Person p = new Student();

Reference variable              object



Upcasting happens if you want to reuse your code that knows about the superclass but not the subclass. 





reference

https://www.youtube.com/watch?v=vp6Mcxc1CTM&t=738s 

https://www.baeldung.com/java-type-casting

'JAVA' 카테고리의 다른 글

Anonymous class in Java  (0) 2019.02.20
What's the difference between two different List declaration?  (0) 2019.02.02
String class in Java  (0) 2019.01.20
Collection framework - ArrayList  (0) 2019.01.17
abstract & Interface in java  (0) 2019.01.17