Object Casting is an Important concept in java which covers upcasting and downcasting in java.In this video, we are going to cover upcasting in depth.We are going to understand the meaning of Parent p = new Child() Statement step by step.We are going to learn how to upcast manually and why explicit upcasting in not necessary in Java.So let’s understand upcasting in java which where we will also cover the dynamic method dispatch concept.
runtime polymorphism in java:
Assume we have a Parent class named ‘Parent’ and a child class named ‘Child’.
Parent p = new Child();
here ‘p’ is parent type reference and 'new child()’ is the runtime object.
So method overriding in java which is also called as runtime polymorphism is generally dealing with runtime object.So in java as we can store child class object in the parent class reference(which is called upcasting).When we call a method with parent type reference(‘p’ in our case), during runtime jvm checks for the runtime object and based on that a specific method get called.It depends on whether the method you are calling has been overridden in the child class. If the method you are calling overridden in the child class, then the child-specific method will get called otherwise the parent-specific method gets called.
So we can say in general in runtime polymorphism in java , object linking happens in the runtime.
rules :
Parent p = new Child(); //upcasting
-
During compilation, the compiler only checks for the method that we are calling is available in the parent class or not.If yes, then compilation successful otherwise we will get compile time error saying the method is not available in the parent reference.So the calling method should be available in the parent class if we are calling the method using the parent reference.
-
If the calling method is overridden in the child calss( inheritance in java ) then the child-specific method will be get called which is called as dynamic method dispatch.
In this video, we will also talk about dynamic dispatch in details with a tons of example.
Object casting in java :
When we talk about upcasting in java, we have to understand that the upcasting is safe and it is implicit.There is no need to do upcasting in java explicitly as jvm does it internally.
for an example :
casting sub type to a supper type is called upcasting :
Child c = new Child();
now is c is a subtype.let’s cast it to supper type
Parent p = (Parent)c;
now we have casted it to supper type which is parent type.Here we have done upcasting manually which is not necessary.so we can directly do
parent p = c ;
because upcasting is implicit, or we can directly do
Parent p = new Child();
remember that p is storing the child class object.
with ‘p’ reference we can only call parent-specific methods but to call child specific method, we have to downcast.
so we are going to talk about downcasting in java in the next video.
Note: - upcasting and downcasting are two important things in java which is used so much in real time.
Important: To perform upcasting and downcasting the two classes must have the relationship or they must extend to each other (inheritance in java);