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 Thread Starvation in Java ?

What are the Java Collection Classes That Implement List Interfac ...

How Can We Find the Memory Usage of JVM From Java Code ?

What is the Difference Between a Cookie and a Session Object in J ...

How Will You Include a Static File in a JSP Page ?