0
0
Javaprogramming~10 mins

Upcasting and downcasting in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Upcasting and downcasting
Create Subclass Object
Upcast to Superclass Reference
Use Superclass Reference
Downcast to Subclass Reference
Use Subclass Reference
End
First, create an object of a subclass. Then, assign it to a superclass reference (upcasting). Later, convert back to subclass reference (downcasting) to access subclass-specific features.
Execution Sample
Java
class Animal {}
class Dog extends Animal {
  void bark() { System.out.println("Woof"); }
}
Animal a = new Dog(); // upcasting
((Dog) a).bark(); // downcasting
This code creates a Dog object, upcasts it to Animal, then downcasts back to Dog to call bark.
Execution Table
StepActionReference TypeObject TypeMethod CalledOutput
1Create Dog objectDogDogNoneNone
2Upcast Dog to AnimalAnimalDogNoneNone
3Call bark() via Animal reference (invalid)AnimalDogN/ACompile-time error if no cast
4Downcast Animal to DogDogDogNoneNone
5Call bark() via Dog referenceDogDogbark()Woof
💡 Execution ends after bark() prints 'Woof'.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
anullDog objectAnimal reference to Dog objectDog reference to Dog objectDog reference to Dog object
Key Moments - 3 Insights
Why can't we call bark() directly on the Animal reference?
Because the Animal class does not have the bark() method. The execution_table row 3 shows calling bark() on Animal reference causes a compile-time error without downcasting.
What does upcasting do to the reference type?
Upcasting changes the reference type to the superclass (Animal) but the actual object remains the subclass (Dog), as shown in execution_table row 2.
Why do we need downcasting before calling subclass methods?
Downcasting tells the compiler the reference is actually the subclass type, allowing access to subclass methods like bark(), as shown in execution_table rows 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the reference type of variable 'a' after step 2?
ADog
BObject
CAnimal
DCannot determine
💡 Hint
Check the 'Reference Type' column at step 2 in execution_table.
At which step does the program actually print 'Woof'?
AStep 4
BStep 5
CStep 3
DStep 2
💡 Hint
Look at the 'Output' column in execution_table to find when 'Woof' is printed.
If we skip downcasting and call bark() on 'a' directly, what happens?
ACompile-time error
BRuntime error
CIt prints 'Woof' anyway
DNothing happens
💡 Hint
Refer to execution_table row 3 where calling bark() on Animal reference without cast is invalid.
Concept Snapshot
Upcasting: Assign subclass object to superclass reference.
Downcasting: Convert superclass reference back to subclass.
Upcasting is automatic; downcasting requires explicit cast.
Subclass methods need downcasting to be accessed.
Avoid ClassCastException by ensuring object type matches cast.
Full Transcript
This visual trace shows how upcasting and downcasting work in Java. First, a Dog object is created. Then it is upcast to an Animal reference, which means the reference type is Animal but the object is still Dog. You cannot call Dog-specific methods like bark() on the Animal reference directly. To call bark(), you must downcast the Animal reference back to Dog. This cast tells the compiler the reference is actually a Dog, allowing access to bark(). The execution table shows each step, including the reference and object types, and when the bark() method is called and prints 'Woof'. Key moments clarify why direct calls on the superclass reference fail and why casting is necessary. The quiz tests understanding of reference types, method calls, and errors without casting.