What is upcasting and downcasting in Java?

Upcasting and downcasting in Java means converting one class to another.

Upcasting

Upcasting means converting child class to parent class:

class Animal {

}

class Dog extends Animal {

}

Dog dog = new Dog();
Animal dogAsAnimal = (Animal)dog; // <-- Downcasting is here!

Downcasting

Downcasting means converting parent class to child class:

class Animal {

}

class Dog extends Animal {

}

Animal dogAsAnimal = new Dog();
Dog dog = (Dog)dogAsAnimal; // <-- Upcasting is here!

Why "up" and "down"?

In traditional class hierarchy diagrams, parent classes are drawn above child classes, thus going from child to parent is Upcasting and from parent to child is Downcasting.