Upcasting and Downcasting

Java permits an object of a subclass type to be treated as an object of any superclass type. This is called upcasting. Upcasting is done automatically, while downcasting must be manually done.

Example:

Cat c1 = new Cat();
Animal a = c1; //automatic upcasting to Animal
Cat c2 = (Cat) a; //manual downcasting back to a Cat

Upcasting is always typesafe but can cause the so called “slicing” problem.

Object Slicing

Object slicing is defined as the conversion of an object into something with less information (typically a superclass)

More detail

If you upcast an object, it will lose all it’s properties, which were inherited. For example, if you cast a Cat to an Animal, it will lose properties inherited from Mammal and Cat. Note, that data will not be lost, you just can’t use it, until you downcast the object to the right level.

Example

class Base {
    void doSomething() {
        System.out.println("Base");
    }
}

class Derived extends Base {
    void doSomething() {
        System.out.println("Derived");
    }
}

public class JavaUpcasting {
    public static void main(String[] args) {
        Base instance = new Derived();
        instance.doSomething();
    }
}

This will output “Derived”.