015-003-003
Downcasting: From Superclass to Subclass
Medium
Problem Description
Downcasting: From Superclass to Subclass
Learning Objective: Understand explicit casting (downcasting) from superclass type variable to subclass type
Overview
Downcast a Dog instance stored in Animal type variable to call Dog-specific methods.
Specifications
- Animal class: speak() method outputs "Animal speaks"
- Dog class: extends Animal, overrides speak() to output "Dog barks"
- Dog-specific method: fetch() outputs "Dog fetches ball"
- Type check with instanceof before downcasting
Input
No input
Output Format
Dog barks
Dog fetches ball
Test Cases
※ Output examples follow programming industry standards
Input:
Expected Output:
Dog barks Dog fetches ball
❌ Some tests failed
Your Solution
Current Mode:● My Code
99
1
2
3
4
5
6
7
8
9
10
›
⌄
⌄
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write your code here
sc.close();
}
}
0 B / 5 MB
You have 9 free executions remaining
