Why Do You Use Upcasting or Downcasting in Java ?

When we want to cast a Sub class to Super class, we use Upcasting. It is also known as widening. Upcasting is always allowed in Java.

When we want to cast a Super class to Sub class, we use Downcasting. It is also known as narrowing.

At times, Downcasting can throw the ClassCastException if it fails the type check.



You May Interest

What is the Main Difference Between a Session and Cookie in JSP ?

What are the Different Types of Events Provided by Spring Framewo ...

How Can We Reference an Unreferenced Object Again in Java ?

Java How To Find The Average Of 10 Numbers Using A While Loop

What are the Thread-safe Classes in Java Collections Framework ?