What is the purpose of Java’s upcasting?
The purpose of Java upcasting is to assign subclass objects to parent class references, allowing parent class references to reference subclass objects. This helps improve code flexibility and scalability. By upcasting, different subclass objects can be treated as parent class objects, allowing for unified use of parent class methods and attributes. This makes the code more versatile, easier to maintain and extend. Furthermore, upcasting also enables polymorphism, where methods are called based on the actual object type at runtime, enhancing code readability and maintainability.