Javatpoint Logo
Javatpoint Logo

Java Cast Object to Class

Java, being an object-oriented programming language, offers robust support for handling objects and their interactions. One important aspect of working with objects is the ability to convert or cast them to different types or classes. Java provides mechanisms for casting objects, allowing developers to change the reference type of an object temporarily. This feature is essential for polymorphism, interface implementation, and general flexibility in Java programming.

Understanding Object Casting

In Java, casting an object involves converting an object from one class type to another, provided there is a valid inheritance relationship between the classes. The casting process allows the program to treat an object as an instance of a different class than its original class. However, it's essential to note that the actual object doesn't change during the casting process; only the reference type changes temporarily.

The casting process can be classified into two main types:

Upcasting (Widening Casting): Upcasting is casting an object to one of its superclasses. Since the object being cast is already an instance of the superclass, it is always safe to upcast an object. No explicit casting is required for upcasting, and it happens implicitly.

Downcasting (Narrowing Casting): Downcasting is casting an object to one of its subclasses. Downcasting requires an explicit cast, as it involves converting an object to a more specific type. Downcasting can lead to a ClassCastException at runtime if the object being cast is not an instance of the target subclass.

Let's take a closer look at both types of casting with examples.

Upcasting in Java

Upcasting is safe because it involves moving from a more specific type to a more general type. Here's an example to illustrate upcasting:

CastingExample.java

In the example above, we have an Animal class and a subclass Dog. The Dog class overrides the makeSound() method. When we upcast the dog object to an Animal reference (Animal animal = dog;), we can still call the makeSound() method, and it will execute the overridden method from the Dog class.

Downcasting in Java

Downcasting requires an explicit cast and can potentially lead to a ClassCastException if the object is not an instance of the target subclass. Here's an example demonstrating downcasting:

CastingExample1.java

In this example, we have an Animal class and a subclass Dog. After upcasting the Dog object to an Animal reference, we downcast it back to a Dog reference using (Dog) animal. Now, we can call both the makeSound() method (inherited from the Animal class) and the fetch() method (specific to the Dog class) without any issues.

Handling ClassCastException

As mentioned earlier, downcasting can lead to a ClassCastException if the object being cast is not an instance of the target subclass. To avoid unexpected runtime exceptions, it's essential to use the instanceof operator before performing downcasting:

By using the instanceof operator, we can determine whether downcasting is safe, preventing potential runtime exceptions.

CastingExample2.java

Output:

Select an animal:
1. Dog
2. Cat
1
Calling the makeSound() method:
Bark! Bark!
Calling the fetch() method:
Fetching the ball!






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA