Complete the code to upcast a Dog object to an Animal reference.
Animal animal = new Dog(); // [1]Upcasting means treating a subclass object as an instance of its superclass. Here, Dog is upcast to Animal.
Complete the code to downcast an Animal reference back to a Dog object.
Dog dog = (Dog) [1];Downcasting requires an explicit cast. Here, the Animal reference named 'animal' is cast back to Dog.
Fix the error in the downcasting code to avoid ClassCastException.
if ([1] instanceof Dog) { Dog dog = (Dog) animal; }
Before downcasting, check if the object referenced by 'animal' is an instance of Dog to avoid errors.
Fill both blanks to create a list of Animals and add a Dog object using upcasting.
List<[1]> animals = new ArrayList<>(); animals.add(new [2]());
The list holds Animal references. Adding a Dog object is upcasting it to Animal.
Fill all three blanks to safely downcast and call a Dog-specific method.
if ([1] instanceof [2]) { [2] dog = ([2]) [3]; dog.bark(); }
Check if 'animal' is a Dog, then downcast it to Dog and call the Dog-specific method bark().