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

preview_player
Показать описание
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();

Parent p = (Parent)c;

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

Varargs in java (with OCJP/Interview questions)

Stay tuned and like my Facebook page for more.

Music :

1)(intro)
Creative Commons — Attribution 3.0 Unported— CC BY 3.0

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

3)NCM Epic Music Ender Guney

Marvel Style / Cinematic Music / Royalty Free

-----

intro template :
Рекомендации по теме
Комментарии
Автор

This is best ever video of typecasting in youtube...thank you so much sir...

codingchannel
Автор

You have a gift. I know this is 6 years old, I hope you have since continued on and found a way to impact even more people with your teaching talents.

michaelhall
Автор

Thank you so much 😀 all my doubts got clear. This is best ever video of typecasting in YouTube...👏

SRUTHIS-wj
Автор

Thank you so much !!! 👏 God bless you!

jobijose
Автор

Thanx fir this video...Finally I got clear idea about upcasting ...Thnx a lot

mauryar
Автор

What a clear cut video very very well understand very good work bro

yogeshpatel
Автор

Thanks a lot Sir ji.. appreciate your efforts! 🇮🇳🙏🌈

aakashakku
Автор

What a explanation!!!all basics are cleared.

rishisweeklydose
Автор

Awesome explaination now I got full clarity on concept..I can visualize things now

comment
Автор

This is what I needed... Explanation to the point... Great bro

AJ-rjee
Автор

well explained thanks a lot for making easy to understand

rsn
Автор

I really appreciate it! it's the best lecture I've ever seen of upcasting & down casting. I had hard time to understand of that. but I got it completely thanks to your thorough explanations! thanks again!!!👍👍👍👍🌹🌹🌹

solnamgung
Автор

36:17 --> Key sentence of the whole talk imho

chaosdesigner
Автор

Best video ever on Upcasting! + Description is lit Thank you, sir!

matilda
Автор

Explanation is very nice on Upcasting, Please upload video on Downcasting also.

RajendraPrasad-yvvs
Автор

Finally i understood this java puzzle ! Thanks a lot !!

abhiatYT
Автор

Thank you so much 😀 almost all doubts got clear👏 God bless you!

anantgavas
Автор

Hi Abhilash pls upload more videos.It is very resourceful.We could knew how it works in real time environment

ramyaaddanki
Автор

Super Amazing Excellent Explanation I have no enough word to express thanks

abhi
Автор

Wow easily understands one of the critical topics. Thanks for your effort Bro.

rajkumarrubert
visit shbcf.ru