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

ความคิดเห็น • 115

  • @BillBarnum
    @BillBarnum  4 ปีที่แล้ว

    📌 Subscribe For The Latest Videos: bit.ly/36H70sy 📌
    .
    💻 All Java Tutorials: bit.ly/JavaTutorialsTH-cam 💻
    .
    🤖 Learn Java In 3 Hours: bit.ly/JavaIn3Hours 🤖

  • @chandan07talreja
    @chandan07talreja 6 ปีที่แล้ว +41

    Awesome man. A visual representation always helps in better understanding. Keep posting more videos.

  • @aminecherif3832
    @aminecherif3832 3 ปีที่แล้ว +5

    After one day trying to understand Casting and finally just 5 minutes explained very clearly to be ok ! thanks !!!

    • @BillBarnum
      @BillBarnum  3 ปีที่แล้ว

      Thanks for the feedback Amine. I'm glad you like the video.

  • @sp_aryan6
    @sp_aryan6 ปีที่แล้ว +1

    from India, watching it the night before exam. thanks a lot for making a short easy to understand video.

    • @BillBarnum
      @BillBarnum  ปีที่แล้ว +1

      Good luck on your exam!

  • @raxkhmanLite
    @raxkhmanLite 3 ปีที่แล้ว +10

    Indeed, this video is one of the best explanation on casting of objects out there!!!

    • @BillBarnum
      @BillBarnum  3 ปีที่แล้ว +2

      Thanks for the feedback. I'm glad you liked the video!

  • @HungryBirdStephenHaokip
    @HungryBirdStephenHaokip 5 ปีที่แล้ว +8

    Thank you bill been stuck here for a long time now... downcasting is clear

  • @CindyMcGuirk
    @CindyMcGuirk 5 ปีที่แล้ว +14

    Exactly what I needed to know at this moment. Thanks, Bill!

  • @bennails3447
    @bennails3447 3 ปีที่แล้ว +5

    Legendary video on the subject. The best've I found on TH-cam so far!

  • @ayoubanbara1685
    @ayoubanbara1685 4 ปีที่แล้ว +3

    thanks you from morocco

  • @anastasijad4830
    @anastasijad4830 3 ปีที่แล้ว +4

    Thank you sir! God bless you

  • @_mantis
    @_mantis 3 ปีที่แล้ว +9

    I am really thankful for this tutorial. Keep up the good work!

    • @BillBarnum
      @BillBarnum  3 ปีที่แล้ว

      Thanks for the feedback! I'm glad that you found it useful.

  • @hiteshchalise3988
    @hiteshchalise3988 5 ปีที่แล้ว +8

    really informative, and perfect explanation, thanks bill.

  • @HighOctaneNews570
    @HighOctaneNews570 ปีที่แล้ว +1

    Finally someone who explains correctly this topic. Thank you Sir.

    • @BillBarnum
      @BillBarnum  ปีที่แล้ว

      Thanks for watching and commenting, Alejandro.

  • @prabhatdahal3416
    @prabhatdahal3416 4 ปีที่แล้ว +4

    Really helpful video man💯💯💯. keep uploading more videos .
    Thank you so much Bill ❤❤

  • @nodararonia1554
    @nodararonia1554 4 ปีที่แล้ว +5

    Thank you!!

  • @abdur1996
    @abdur1996 3 ปีที่แล้ว +3

    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

    • @BillBarnum
      @BillBarnum  3 ปีที่แล้ว

      Thanks for the feedback!

  • @zackaoz1034
    @zackaoz1034 5 ปีที่แล้ว +7

    I love you Bill, finally a full tutorial with all possible examples.

  • @abdulaziz_xi
    @abdulaziz_xi 4 ปีที่แล้ว +3

    Thanks man … Great explanation

  • @serhatgundem
    @serhatgundem 3 ปีที่แล้ว +2

    Thanks a looooooot. You are great.

    • @BillBarnum
      @BillBarnum  3 ปีที่แล้ว

      Thanks for the feedback Serhat. I'm had you liked the video!

  • @user-lf3yj5zb2r
    @user-lf3yj5zb2r ปีที่แล้ว

    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

  • @langstonhodges5863
    @langstonhodges5863 4 ปีที่แล้ว +2

    Do these same concepts hold true with the fields for the classes when upcasting and downcasting? This video only covers the interactions with methods.

    • @BillBarnum
      @BillBarnum  4 ปีที่แล้ว

      Yes, it would work the same way with fields.

    • @langstonhodges5863
      @langstonhodges5863 4 ปีที่แล้ว +1

      Bill Barnum Thank you very much it all makes since now.

  • @Astrovic1
    @Astrovic1 3 ปีที่แล้ว +1

    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

    • @BillBarnum
      @BillBarnum  3 ปีที่แล้ว

      Good luck on your exam!

  • @mosesmuchemi7221
    @mosesmuchemi7221 4 ปีที่แล้ว +1

    Lifesaver

  • @kbuvanesh5347
    @kbuvanesh5347 ปีที่แล้ว

    Very good explaination sir love ❤️ from India 🇮🇳

  • @rho8931
    @rho8931 ปีที่แล้ว +1

    Thank you so much for this awesome tutorial!

    • @BillBarnum
      @BillBarnum  ปีที่แล้ว

      Glad it was helpful, Rhoad!

  • @SoftwareTestSolutions
    @SoftwareTestSolutions 3 ปีที่แล้ว +1

    That is a great explanation... Thanks for the video!

  • @louishou781
    @louishou781 5 ปีที่แล้ว +3

    this video is awesome - thank you so much

  • @zhenyuanhe6071
    @zhenyuanhe6071 4 ปีที่แล้ว +2

    Thanks a lot! Clear enough:)

  • @joaovalerio8137
    @joaovalerio8137 2 ปีที่แล้ว +1

    Brilliant explanation!

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว

      Thanks for watching and commenting, João!

  • @rajdhar1235
    @rajdhar1235 8 หลายเดือนก่อน

    Amazing explanation. Thank you.

  • @criquetteable
    @criquetteable 5 ปีที่แล้ว +4

    really good!
    perfect explanation
    thanks a lot

  • @robertastutlys8641
    @robertastutlys8641 4 ปีที่แล้ว +2

    Exactly what i needed. Thanks for this great content!

  • @user-lf3yj5zb2r
    @user-lf3yj5zb2r 2 ปีที่แล้ว +1

    OMGGGG!!! You are a genius!!! It's awesome =)

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว

      Thanks for watching and commenting!

  • @jackwesh2976
    @jackwesh2976 3 ปีที่แล้ว +1

    Loved it Thanks Much

    • @BillBarnum
      @BillBarnum  3 ปีที่แล้ว +1

      Thanks for the feedback!

  • @real_hello_kitty
    @real_hello_kitty 4 ปีที่แล้ว +1

    Thanks.👍

  • @user-br9gz5qq1r
    @user-br9gz5qq1r 5 ปีที่แล้ว +4

    you're the best, keep it up

  • @s2k_love492
    @s2k_love492 4 ปีที่แล้ว +2

    Excellent, helped a lot. Thank you!

  • @funnybunny3188
    @funnybunny3188 2 ปีที่แล้ว +1

    Very good explanation i got my points

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว

      Thanks for watching and commenting.

  • @18DZ
    @18DZ 3 ปีที่แล้ว +1

    Exactly what I needed before my exam!

  • @obit6380
    @obit6380 3 ปีที่แล้ว +1

    Thanks for the video! Very clear and to the point presentation!

    • @BillBarnum
      @BillBarnum  3 ปีที่แล้ว

      Thanks for the feedback, OBIT.

  • @AyumiNoran
    @AyumiNoran 3 ปีที่แล้ว +2

    Thank you!

    • @BillBarnum
      @BillBarnum  3 ปีที่แล้ว +1

      Thanks for the feedback. I'm glad you found it helpful.

  • @maharun0
    @maharun0 ปีที่แล้ว

    I have a serious confusion. Here it is:
    Animal

    • @BillBarnum
      @BillBarnum  ปีที่แล้ว

      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?

  • @yalakaturi.saidivya9158
    @yalakaturi.saidivya9158 ปีที่แล้ว +1

    Mutilple times of casting,by using single object is possible or not?

    • @BillBarnum
      @BillBarnum  ปีที่แล้ว

      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.

  • @rafaybbx
    @rafaybbx ปีที่แล้ว +1

    amazing explanation

  • @IluSsIoNnN
    @IluSsIoNnN 2 ปีที่แล้ว +1

    Thank you my good sir !!!!

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว

      Thanks for the comment, IluSsIoNnN.

  • @johncerpa3782
    @johncerpa3782 4 ปีที่แล้ว +1

    Great video

  • @sweeren3619
    @sweeren3619 4 ปีที่แล้ว +1

    ty so much

  • @camnooten110
    @camnooten110 4 ปีที่แล้ว +1

    Thank you, this really helps

  • @kakashi99908
    @kakashi99908 2 ปีที่แล้ว +1

    So what are the limitations of doing: Lion jen = new Lion()?

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว

      I recommend my video on Polymorphism. I think it will answer your question:
      th-cam.com/video/cUh3Q9l3Q-0/w-d-xo.html

    • @kakashi99908
      @kakashi99908 2 ปีที่แล้ว

      ​@@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.

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว +1

      ​@@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.

  • @ginopaolettigeuna1946
    @ginopaolettigeuna1946 3 ปีที่แล้ว +1

    excellent!

    • @BillBarnum
      @BillBarnum  3 ปีที่แล้ว +1

      Thanks for the feedback Gino.

  • @ramzirebai3661
    @ramzirebai3661 2 ปีที่แล้ว +1

    Thank you so much

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว

      Thanks for watching and commenting Ramzi!

  • @lazry1773
    @lazry1773 2 ปีที่แล้ว +1

    great video

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว

      Thanks for watching and commenting, Lazry!

  • @jsonwann8908
    @jsonwann8908 4 ปีที่แล้ว

    thanks...

  • @Subhash_VCU
    @Subhash_VCU ปีที่แล้ว

    Animal jen = new lion()
    Jen. Controltemp() , a valid statement??

  • @younesred112
    @younesred112 4 ปีที่แล้ว

    Thank you so much !! awesome explanition

  • @ash2521
    @ash2521 2 ปีที่แล้ว +1

    very descriptive

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว +1

      Thanks for watching and the comment, Aashish!

  • @jangul8230
    @jangul8230 5 ปีที่แล้ว +1

    v.good sir

  • @spencerrobles9373
    @spencerrobles9373 ปีที่แล้ว

    Good video

  • @adityakuricheti6653
    @adityakuricheti6653 2 ปีที่แล้ว +1

    lion l = new lion();
    This is valid
    So will it come under downcasting?

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว

      Good question.
      In that case, the variable and object type are the same, so it isn't casting.

    • @adityakuricheti6653
      @adityakuricheti6653 2 ปีที่แล้ว +1

      @@BillBarnum ok thanks

  • @asitkumarsahu5211
    @asitkumarsahu5211 2 ปีที่แล้ว

    How to cast string to user defined object type

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว

      Here are a couple of examples:
      Object x = new String("test 123"); //upcasting example
      String y = (String)x; //downcasting example

  • @Aloksheoran
    @Aloksheoran 3 ปีที่แล้ว

    Good for mugging up only

  • @adriansalabanzi2250
    @adriansalabanzi2250 5 ปีที่แล้ว +1

    cool.

  • @wasimpatel2618
    @wasimpatel2618 2 ปีที่แล้ว

    i cant hear the sentence at 4:02. can anybody tell me pls.

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว +1

      "This is legal because we are assuring the compiler that Jen is, in fact, pointing at a Lion type object. "

    • @wasimpatel2618
      @wasimpatel2618 2 ปีที่แล้ว

      @@BillBarnum thank you so much sir for explaining, I misheard is, in fact to "isn't fact ".

  • @simongadd8545
    @simongadd8545 2 ปีที่แล้ว +1

    Explained an hour of university lecture content in just under 6 minutes and so much better. Maybe I should pay you instead?

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว

      Thanks for the comment, Simon.
      I'm glad you thought it was a useful video!

  • @xrealmob_1037
    @xrealmob_1037 4 ปีที่แล้ว

    tx G

  • @barshabhattacharjee6788
    @barshabhattacharjee6788 3 ปีที่แล้ว

    For more visit: th-cam.com/video/wbrWpt--cgw/w-d-xo.html

  • @khorLDW
    @khorLDW 4 ปีที่แล้ว +2

    poor joe

    • @BillBarnum
      @BillBarnum  4 ปีที่แล้ว +3

      That's what happens when you don't follow the rules of Java. 🙂

  • @user-lf3yj5zb2r
    @user-lf3yj5zb2r ปีที่แล้ว

    +

  • @mehmetali627
    @mehmetali627 ปีที่แล้ว +2

    Good job bro

    • @BillBarnum
      @BillBarnum  ปีที่แล้ว +1

      Thanks

    • @mehmetali627
      @mehmetali627 ปีที่แล้ว

      @@BillBarnum you are welcome sir. You've saved my life in college.

  • @user-xr7pk2fy4x
    @user-xr7pk2fy4x 4 ปีที่แล้ว +4

    Thanks a lot!

  • @anantshukla6395
    @anantshukla6395 2 ปีที่แล้ว +1

    Awesome video

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว

      Thanks for watching and commenting, Anant!

  • @Carlitos_SH
    @Carlitos_SH 3 ปีที่แล้ว

    THANK YOU !

  • @Xtcent
    @Xtcent 3 ปีที่แล้ว +1

    Great video

    • @BillBarnum
      @BillBarnum  3 ปีที่แล้ว

      Thanks for checking out the video!

  • @mahaaftab755
    @mahaaftab755 2 ปีที่แล้ว +1

    great. thanks!

    • @BillBarnum
      @BillBarnum  2 ปีที่แล้ว

      Thanks for commenting, Maha!