Challenge - 5 Problems
Casting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateOutput of Upcasting and Method Calls
What is the output of this Java code snippet?
Java
class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { void sound() { System.out.println("Dog barks"); } void fetch() { System.out.println("Dog fetches ball"); } } public class Test { public static void main(String[] args) { Animal a = new Dog(); // upcasting a.sound(); } }
Attempts:
2 left
π‘ Hint
Remember that overridden methods use the actual object's method, even if referenced by a parent type.
β Incorrect
The variable 'a' is of type Animal but refers to a Dog object. The sound() method is overridden in Dog, so Dog's version runs.
β Predict Output
intermediateDowncasting and Method Access
What will be the output of this Java code?
Java
class Vehicle { void start() { System.out.println("Vehicle started"); } } class Car extends Vehicle { void openSunroof() { System.out.println("Sunroof opened"); } } public class Test { public static void main(String[] args) { Vehicle v = new Car(); // upcasting ((Car) v).openSunroof(); // downcasting } }
Attempts:
2 left
π‘ Hint
Downcasting allows access to subclass-specific methods.
β Incorrect
The object 'v' is actually a Car, so downcasting to Car is safe and allows calling openSunroof().
β Predict Output
advancedRuntime Error from Invalid Downcasting
What error does this code produce when run?
Java
class Fruit { void eat() { System.out.println("Eating fruit"); } } class Apple extends Fruit { void peel() { System.out.println("Peeling apple"); } } class Orange extends Fruit { void peel() { System.out.println("Peeling orange"); } } public class Test { public static void main(String[] args) { Fruit f = new Orange(); Apple a = (Apple) f; // downcasting a.peel(); } }
Attempts:
2 left
π‘ Hint
Downcasting to a wrong subclass causes runtime errors.
β Incorrect
The object is an Orange but cast to Apple, causing ClassCastException at runtime.
π§ Conceptual
advancedUnderstanding Upcasting Behavior
Given the code below, what will be the output when main runs?
Java
class Parent { void show() { System.out.println("Parent show"); } } class Child extends Parent { void show() { System.out.println("Child show"); } void play() { System.out.println("Child play"); } } public class Test { public static void main(String[] args) { Parent p = new Child(); // upcasting p.show(); // p.play(); // commented out } }
Attempts:
2 left
π‘ Hint
Upcasting hides subclass-only methods but overridden methods run subclass versions.
β Incorrect
The show() method is overridden, so Child's version runs. The play() method is not accessible via Parent reference.
β Predict Output
expertOutput and Behavior of Complex Casting
What is the output of this Java program?
Java
class A { void m() { System.out.println("A.m()"); } } class B extends A { void m() { System.out.println("B.m()"); } void n() { System.out.println("B.n()"); } } class C extends B { void m() { System.out.println("C.m()"); } void n() { System.out.println("C.n()"); } } public class Test { public static void main(String[] args) { A obj = new C(); // upcasting obj.m(); ((B) obj).n(); // downcasting } }
Attempts:
2 left
π‘ Hint
Method calls use the actual object's version, even after casting.
β Incorrect
obj refers to a C object. m() is overridden in C, so C.m() runs. Downcasting to B allows calling n(), but n() is overridden in C, so C.n() runs.
