This material is licensed under the Creative Commons BY-NC-SA license, which means that you can use it and distribute it freely so long as you do not erase the names of the original authors. If you make changes in the material and want to distribute this altered version of the material, you have to license it with a similar free license. The use of the material for commercial use is prohibited without a separate agreement.
Authors: Arto Hellas, Matti Luukkainen
Translators to English: Emilia Hjelm, Alex H. Virtanen, Matti Luukkainen, Virpi Sumu, Birunthan Mohanathas, Etiënne Goossens
Extra material added by: Etiënne Goossens, Maurice Snoeren, Johan Talboom
The course is maintained by Technische Informatica Breda
We’ve seen casting before, where we had a double, but wanted to store it in a integer variable. We can also cast objects. In our previous example of the animals, a Cow
is also an Animal
, so we can store it, but not all Animal
s are Cow
s, so we can’t store it that way. Let’s look at an example
Animal animal = new Cow(); // valid
Cow cow = (Cow)animal; // valid
Pig pig = (Pig)animal; // runtime exception
We can use casting on objects, but this casting is not always valid. If we try to cast a Cow
object to a Pig
object, java will give an error, as this is not supported behaviour.
Casting is only allowed to the right types, and to the sub or superclasses of an object. We can however, test if an object is an instance of a certain class.
Animal animal = (Animal) new Cow()
, this is done automaticallyCow
object, stored in an Animal
variable, can be casted to a Cow
, but not to a Pig
If we want to perform downcasting, we need to check what the type of the object is. We can do this using the instanceOf
operator
Animal animal = new Cow();
if(animal instanceOf Cow) {
System.out.println("This is a cow");
Cow cow = (Cow)animal;
//call specific cow method
} else if(animal instanceOf Pig) {
System.out.println("This is a pig");
}
as you can see in this example, we could test every object to see what type it is, casting it, and then calling methods on that object. This is however usually a sign of a bad design, and should be avoided as much as possible. This is why there will be no exercises for casting