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 are the Examples of Observer Design Pattern in JDK ?

What are the Differences Between a Vector and an ArrayList in Jav ...

What is a Directive in JSP ?

How Can We Take Thread Dump in Java ?

What is the Difference Between Sleep and Wait Methods in Java ?