JAVA

Upcasting (Object type casting) in java

seoca 2019. 1. 30. 10:02




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