Upcasting vs Downcasting in Java With Program Example

In this java programming video tutorial we will study and understand the concept of Upcasting and Downcasting. We will also understand the difference between upcasting and downcasting in Java.
Upcasting in Java –
Upcasting is casting a subtype to a supertype, upward to the inheritance tree. Upcasting happens automatically and we donโ€™t have to explicitly do anything.

Downcasting in Java –
When Subclass type refers to the object of Parent class, it is known as downcasting. If we perform it…

18 thoughts on “Upcasting vs Downcasting in Java With Program Example

  1. Hey Guys, if you want more such tech educational videos on this channel then please support me by subscribing to this channel & also share it with your friends as it helps me create more content just for you โœŒ

  2. very well explained and the blog is also super good …. actually when i read some more articles i found the blog ie simplesnippets better then g4g or jvtpoint

  3. hey I have a question regarding your real-time example of upcasting you mentioned "Upcasting is used when we want to refer multiple child classes by a single parent object N we want to call their respective overridden methods at a particular time when necessary"
    Could you please elaborate specifically on what did you mean by referencing them at a particular time when necessary?
    let's say we have 2 child classes child() & child1()
    N we create Parent p1=new child();
    so my question is do we need to create another Parent class object ref Parent p2=new child1(); for child1()? If that's the case then let's say we have 20 child methods for each of them we need to create p3….p20 then where is the benefit here of using upcasting?

    I just want to know from the perspective of benefits of using upcasting when we have multiple child classes

    I hope I am clear ๐Ÿ™‚ waiting for your response

Leave a Reply

Your email address will not be published. Required fields are marked *