Explore object casting in java | parent p = new child() | upcasting with dynamic method dispatch

Описание к видео Explore object casting in java | parent p = new child() | upcasting with dynamic method dispatch

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

1) 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.

2) 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);



About this video :
This video covers upcasting in java with examples.We will go through different examples in upcasting and also we will do some exercise at the end of the tutorial which will make you comfortable with upcasting.By the end of this tutorial, we will get a good idea about casting objects in java.We will understand, we can store child class object with parent class reference.

So if you are looking for few tutorials on inheritance in java with examples then this video is for you.We will cover a lot of java inheritance program example and we will understand runtime polymorphism in java and dynamic method dispatch with all in one video.

//////

few of my other tutorials :

Why String is immutable in Java ? || The four reason you must know

   • Why String is immutable in Java ? || ...  

Varargs in java (with OCJP/Interview questions)

   • Varargs in java (with OCJP/Interview ...  

Stay tuned and like my Facebook page for more.

  / seleniumexpress  



Music :

1)(intro)
Adventures by A Himitsu   / a-himitsu  
Creative Commons — Attribution 3.0 Unported— CC BY 3.0
http://creativecommons.org/licenses/b...
Music released by Argofox    • A Himitsu - Adventures [Royalty Free ...  
Music provided by Audio Library    • Adventures – A Himitsu (No Copyright ...  


2)A Himitsu - Smile (Harbinger Remix) [Creative Commons]
   • A Himitsu - Smile (Harbinger Remix) [...  

3)NCM Epic Music Ender Guney
   / ncmepicmusic  

Marvel Style / Cinematic Music / Royalty Free
   • Видео  


-----

intro template :
wwww.youtube.com/Alexbau01

Комментарии

Информация по комментариям в разработке