Skip to main content

Section 3.2 Typecasting

Typecasting is the process of converting a variable from one type to another. In Java, this is often necessary when you want to perform operations that require different data types. For example, if you have an integer and you want to convert it to a double for more precise calculations, you would use typecasting.
In Java, typecasting can be done in two ways: implicit and explicit. Implicit typecasting occurs automatically when converting from a smaller data type to a larger one (like int to double), while explicit typecasting requires you to specify the conversion manually (like double to int).
Implicit typecasting happens automatically when converting a value from a smaller data type to a larger one, as there is no risk of losing information. For example, you can assign an int to a double without any special syntax as shown in Listingย 3.2.1.
Listing 3.2.1.
Explicit typecasting is required when converting from a larger data type to a smaller one, as you might lose data. You must do this manually by placing the target type in parentheses () before the value as shown in Listingย 3.2.2.
Listing 3.2.2.
Besides primitive types, type casting is also a fundamental concept when working with objects, especially within an inheritance hierarchy. This involves converting an object reference from one class type to another, typically between a superclass and a subclass. This is often referred to as upcasting and downcasting.
In Listingย 3.2.3, we have a simple class hierarchy: an Animal superclass and a Dog subclass.
Listing 3.2.3.
/**
    * Animal class is the superclass, with a method makeSound().
    */        
class Animal {
    public void makeSound() {
        System.out.println("The animal makes a sound.");
    }
}

/**
    * Dog class is a subclass of Animal, with an additional method bark().
    */
class Dog extends Animal {
    public void bark() {
        System.out.println("The dog barks!");
    }
}
Upcasting (Implicit): Upcasting is casting a subclass instance to a superclass reference type. This is always safe because a subclass object is guaranteed to have all the methods and properties of its superclass. Therefore, upcasting is done implicitly by the compiler as Listingย 3.2.4.
Listing 3.2.4.
Downcasting (Explicit): Downcasting is casting a superclass reference back to its original subclass type. This is potentially unsafe because the superclass reference might not actually point to an object of the target subclass. You must perform an explicit cast. If you cast to the wrong type, Java will throw a ClassCastException at runtime.
To safely downcast, you should first check the objectโ€™s type using the instanceof operator as shown in Listingย 3.2.5.
Listing 3.2.5.
In Listingย 3.2.5, we first create a Dog object and assign it to an Animal reference (upcasting). Then, we check if the Animal reference is actually pointing to a Dog object before downcasting it back to a Dog reference.

Checkpoint 3.2.6.

Construct a program that safely downcasts a Shape reference to a Circle object and calls a subclass method. Drag the blocks into the correct order on the right.
You have attempted of activities on this page.