Java Casting

Summary: in this tutorial, you will learn how to use Java casting that convert an object of a class to another.

Introduction to Java Casting

In Java, casting is a technique that converts an object of a class into another. Casting allows you to access fields and methods that are specific to a target class.

Technically, you should only use casting when there is an actual relationship between the classes in the class hierarchy. It means you can only cast an object of classes that have a superclass-subclass relationship.

If you attempt to cast unrelated classes, you will encounter an error runtime. More specifically, you’ll get an ClassCastException.

Java supports two types of object castings: upcasting and downcasting.

Upcasting

Upcasting converts an object of a subclass to its superclass:

Superclass object = new Subclass(); // upcastingCode language: Java (java)

In this syntax, we create a new object of a subclass and assign it to a reference variable with the type of Superclass. The upcasting is always safe and doesn’t require an explicit cast.

In the following syntax, we assign an instance of a subclass to a reference of a superclass:

Superclass object = subclassObject; // upcastingCode language: Java (java)

Downcasting

Downcasting is the casting of an object of a superclass to a subclass:

Subclass subObj = (Subclass) superObj; // DowncastingCode language: Java (java)

Note that you can only perform a downcasting if the original object is of the subclass type. And you need to use parentheses and the subclass type.

For safety, you can use the instanceof operator to check if the object being downcasted is an instance of the Subclass:

if (obj instanceof Subclass) {
    Subclass subObj = (Subclass) obj; // Downcast only if safe
}Code language: Java (java)

Java casting example

Suppose you have two classes, Person and Employee. The Employee class is a subclass of the Person class.

// Person.java
class Person {
  // ...
}

// Employee.java
class Employee extends Person {
  // ...
}Code language: Java (java)

The following example illustrates how to perform an upcasting that converts an object of the Employee class to the Person class:

Person person = new Employee();Code language: Java (java)

In this case, the person object can access only fields and methods of the Person class. And the person object cannot access the fields and methods defined in the Employee class.

The following example shows how to perform a downcasting that converts

Person person = new Employee();
if (person instanceof Employee) {
    Employee employee = (Employee) person;
}Code language: Java (java)

Because it is a downcasting, we use the instanceof operator to ensure that the person is an instance of the Employee class and use explicit cast by placing the Employee class in parentheses.

Because of downcasting, the employee can access the fields and methods defined in the Employee class.

Summary

  • Use Java casting to convert an object of one class to another, within the class hierarchy.
  • Use downcasting to convert an object of a superclass to its subclass.
  • Use upcasting to convert an object of a subclass to its superclass.