Casting an Object (Upcasting and Downcasting) - Java Tutorial
ฝัง
- เผยแพร่เมื่อ 6 ก.ย. 2024
- Learn about casting an object in Java. Visual examples of upcasting and downcasting.
Aligned to AP Computer Science A. Part of Object Oriented Programming Java (Java OOP).
🔥 Subscribe To Get More Tutorials: bit.ly/36H70sy 🔥
#Java #JavaTutorial #JavaTutorials
📌 Subscribe For The Latest Videos: bit.ly/36H70sy 📌
.
💻 All Java Tutorials: bit.ly/JavaTutorialsTH-cam 💻
.
🤖 Learn Java In 3 Hours: bit.ly/JavaIn3Hours 🤖
Awesome man. A visual representation always helps in better understanding. Keep posting more videos.
After one day trying to understand Casting and finally just 5 minutes explained very clearly to be ok ! thanks !!!
Thanks for the feedback Amine. I'm glad you like the video.
from India, watching it the night before exam. thanks a lot for making a short easy to understand video.
Good luck on your exam!
Indeed, this video is one of the best explanation on casting of objects out there!!!
Thanks for the feedback. I'm glad you liked the video!
Thank you bill been stuck here for a long time now... downcasting is clear
Exactly what I needed to know at this moment. Thanks, Bill!
Legendary video on the subject. The best've I found on TH-cam so far!
Wow, thanks!
thanks you from morocco
Thank you sir! God bless you
I am really thankful for this tutorial. Keep up the good work!
Thanks for the feedback! I'm glad that you found it useful.
really informative, and perfect explanation, thanks bill.
Finally someone who explains correctly this topic. Thank you Sir.
Thanks for watching and commenting, Alejandro.
Really helpful video man💯💯💯. keep uploading more videos .
Thank you so much Bill ❤❤
Thank you!!
Was looking for C# but this was very relavent regardless. I really really like the stack and heap insights in the tutorial as it helps understand whats *really* going on
Thanks for the feedback!
I love you Bill, finally a full tutorial with all possible examples.
Thanks man … Great explanation
Thanks a looooooot. You are great.
Thanks for the feedback Serhat. I'm had you liked the video!
01:10 does not Lion class inherit controlTemp() as well? If we talk about general inheritance as it is.
So, in total Lion method has access to next method
(via inheritance)
eat() - from Animal
sleep() - from Animal
controlTemp() - from mammal
and one own unique method
roar()
ear() @overridden
Do these same concepts hold true with the fields for the classes when upcasting and downcasting? This video only covers the interactions with methods.
Yes, it would work the same way with fields.
Bill Barnum Thank you very much it all makes since now.
it is midnight and will be writing an important exam at 8am :D after this video i finally got the object casting thank you haha :D
Good luck on your exam!
Lifesaver
Very good explaination sir love ❤️ from India 🇮🇳
Thank you so much for this awesome tutorial!
Glad it was helpful, Rhoad!
That is a great explanation... Thanks for the video!
this video is awesome - thank you so much
Thanks a lot! Clear enough:)
Brilliant explanation!
Thanks for watching and commenting, João!
Amazing explanation. Thank you.
really good!
perfect explanation
thanks a lot
Exactly what i needed. Thanks for this great content!
OMGGGG!!! You are a genius!!! It's awesome =)
Thanks for watching and commenting!
Loved it Thanks Much
Thanks for the feedback!
Thanks.👍
you're the best, keep it up
Excellent, helped a lot. Thank you!
Very good explanation i got my points
Thanks for watching and commenting.
Exactly what I needed before my exam!
Thanks for the video! Very clear and to the point presentation!
Thanks for the feedback, OBIT.
Thank you!
Thanks for the feedback. I'm glad you found it helpful.
I have a serious confusion. Here it is:
Animal
You can't override the sleep() function in the Animal class because that's where it originates.
You can override sleep in a subclass that would otherwise inherit sleep()
Does that answer your question?
Mutilple times of casting,by using single object is possible or not?
Yes. When you cast an object, you aren't changing any data. You are just "making promises" to the compiler that it is pointing at an object of the type you are casting it as.
amazing explanation
Glad you liked it
Thank you my good sir !!!!
Thanks for the comment, IluSsIoNnN.
Great video
ty so much
Thank you, this really helps
So what are the limitations of doing: Lion jen = new Lion()?
I recommend my video on Polymorphism. I think it will answer your question:
th-cam.com/video/cUh3Q9l3Q-0/w-d-xo.html
@@BillBarnum I watched it but am still a little confused. In the classes Lion extends mammal, mammal extends animal so wouldn't Lion jen = new Lion() work without having to type cast any methods?
Basically I am confused when you use Animal jen in place of Lion jen because they seem to do the same thing.
@@kakashi99908
Let's take two variables:
Animal alex = new Lion();
Lion jen = new Lion();
The methods available to alex are only those that are declared in Animal. So we could do alex.eat() and alex.sleep(), but we couldn't do alex.roar(), because the alex variable is an Animal and there is no roar() in the Animal class.
On the other hand, the jen variable is declared as a Lion. Lion classes have the roar() method, so we COULD do jen.roar()
The variable type determines what methods are available. The object type determines what version of the method is run.
excellent!
Thanks for the feedback Gino.
Thank you so much
Thanks for watching and commenting Ramzi!
great video
Thanks for watching and commenting, Lazry!
thanks...
Animal jen = new lion()
Jen. Controltemp() , a valid statement??
Thank you so much !! awesome explanition
very descriptive
Thanks for watching and the comment, Aashish!
v.good sir
Good video
lion l = new lion();
This is valid
So will it come under downcasting?
Good question.
In that case, the variable and object type are the same, so it isn't casting.
@@BillBarnum ok thanks
How to cast string to user defined object type
Here are a couple of examples:
Object x = new String("test 123"); //upcasting example
String y = (String)x; //downcasting example
Good for mugging up only
cool.
i cant hear the sentence at 4:02. can anybody tell me pls.
"This is legal because we are assuring the compiler that Jen is, in fact, pointing at a Lion type object. "
@@BillBarnum thank you so much sir for explaining, I misheard is, in fact to "isn't fact ".
Explained an hour of university lecture content in just under 6 minutes and so much better. Maybe I should pay you instead?
Thanks for the comment, Simon.
I'm glad you thought it was a useful video!
tx G
For more visit: th-cam.com/video/wbrWpt--cgw/w-d-xo.html
poor joe
That's what happens when you don't follow the rules of Java. 🙂
+
Good job bro
Thanks
@@BillBarnum you are welcome sir. You've saved my life in college.
Thanks a lot!
Awesome video
Thanks for watching and commenting, Anant!
THANK YOU !
Great video
Thanks for checking out the video!
great. thanks!
Thanks for commenting, Maha!