Casting in Java

Casting is not the most interesting subject in programming, but it is nevertheless very important to be familiar with it.
In this article I’ll give you a quick introduction to casting in Java, as well as some examples on how to use casting in Java.

What is casting in Java?

Casting in Java means that you take one data type (either reference type or primitive) and transform it into another data type.
Said in other words, when you are casting in Java you are simply converting between two different reference types / primitives.

The syntax for casting in Java is simple;

DataType myVariable = (DataType) otherVariable;

On the left side of the equals sign we declare myVariable as a variable of type DataType.
The actual casting happens on the right hand side of the equals sign.
The statement on the right side will transform otherVariable into a variable of type DataType.

With that said, you can’t simply cast between any variables – they must have some form of relationship.
More specifically, the objects must be connected to each other either through inheritance or interfaces.

We will now go through three scenarios for casting in Java – upcasting, downcasting and casting through interfaces.

In our examples we will be working with two classes – Animal and Dog, where the Dog class will extend the Animal class.

Tutorial on Casting in Java

From our simple diagram we can see that the Dog class extends the Animal class.

Upcasting in Java

The simplest form of casting is definitely upcasting.

Upcasting simply means that you transform the object to be less specific.
Said in other words, if one class extends from another, and you cast the child class to the parent class, you are upcasting.

In this scenario, upcasting is when you transform an object of type Dog into an object of type Animal.
The Dog object is now less specific – it is only an Animal.

When you are upcasting from Dog to Animal, all of the information stored in the Dog class will be lost.

Downcasting in Java

Downcasting is simply the opposite of upcasting.

When downcasting, you are making the object more specific.
Going back to our example, this translates to transforming an object of type Animal into an object of type Dog.

As you can see on line 1, we initialize the Animal object to be a Dog.
It is therefore perfectly fine to downcast it into a Dog later on.

Now, what if we introduce a third class, Cat?

What will happen if we try to cast an Animal which is actually a Dog into an object of type Cat?

The compiler won’t give you any errors, but this will fail at runtime with a ClassCastException.
The lesson to take home from this is that you need to be very careful when casting in Java, and always make sure that the object you are trying to cast to is compatible!

Interface casting in Java

Casting is also relevant when working with interfaces.
Now, let’s just focus on the Animal class, and introduce an interface, NormalAnimal.

Our Animal class will implement this interface.

Now, since our Animal class implements NormalAnimal, we can cast from Animal to NormalAnimal.

The Animal class IS-A NormalAnimal, so it is perfectly fine to cast the object into the interface.

Keep in mind that the cast to the interface is actually redundant (not needed).
The Java Virtual Machine will in practice perform this cast for you, so it is perfectly fine to edit line 2 to this:

NormalAnimal normalAnimal = animal;

Wrapping up

This article was a short introduction to casting in Java.
If you struggle to remember the difference between upcasting and downcasting, just remember this:

Upcasting will make the object less specific, while downcasting will make the object more specific.

Happy coding 😉

Quick tip: If you wish to expand your Java skill-set and become an advanced Java developer, I highly recommend you to get yourself a copy of the best-selling Effective Java, by Joshua Bloch!

You may also like...

1 Response

  1. Hello AOProgrammer,

    Nice blog! I am editor at Java Code Geeks (www.javacodegeeks.com). We have the JCG program (see http://www.javacodegeeks.com/join-us/jcg/), that I think you’d be perfect for.

    If you’re interested, send me an email to eleftheria.drosopoulou@javacodegeeks.com and we can discuss further.

    Best regards,
    Eleftheria Drosopoulou

Leave a Reply

Your email address will not be published. Required fields are marked *