Basic concepts in Java Upcasting and Downcasting

Java Upcasting and Downcasting 

 

1.Upcasting :-


    The process of assigning an object from the subclass to the variable from the superclass reference can be called upcasting. In upcasting child class will runs always.


    To put it another way, it's a method of handling a child class object as if it belonged to its parent class. The Java Virtual Machine (JVM) does this conversion implicitly during runtime.

    The child class object loses its unique properties and methods that are absent from its parent class when upcasting is done. The methods and properties declared in the parent class are the only ones that the resultant object can access.

    When you wish to refer to objects of multiple child classes using a single reference type, upcasting comes in handy. As a result, you can build more generic code that works with a variety of object kinds without needing to write unique code for each kind.




2.Downcasting :-

    Downcasting is the conversion of the child class assigned to a variable of the parent class back into a child class variable.
    

    If we want to do downcasting, we have to do upcasting.     We cant do downcasting directly.



Comments

Popular posts from this blog

අද අපි කතා කරමු Data collections ගැන

Understanding and Mastering Coding Errors

OOP key points