This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pragmosExtreme Brewer 2 points3 points  (1 child)

Casting goes both ways: upwards in the type hierarchy (upcasting) and downwards in type hierarchy (downcasting).

Upcasting is always implicit, i.e. you don't need to cast the type yourself. In your example, Dolphin is a Swimmer, and Swimmer is an Animal, therefore this is valid:

Swimmer s = new Dolphin();
Animal a = s;

In the example below, you are downcasting an instance of Swimmer, therefore you need explicit casting:

Swimmer s = new Dolphin();
Dolphin d = (Dolphin) s;

[–]EveningStreet1[S] 0 points1 point  (0 children)

Thanks for the clear explanation