Oracle
1Z0-819 · Question #143
Given: public class Test { public static void main(String[] args) { AnotherClass ac = new AnotherClass(); SomeClass sc = new AnotherClass(); ac = sc; sc.methodA(); ac.methodA(); } } class SomeClass…
The correct answer is C. The compilation fails. You've hit your session limit · resets 1:50am (America/New_York)
Java Object-Oriented Approach
Question
Given:
public class Test {
public static void main(String[] args) {
AnotherClass ac = new AnotherClass();
SomeClass sc = new AnotherClass();
ac = sc;
sc.methodA();
ac.methodA();
}
}
class SomeClass {
public void methodA() {
System.out.println("SomeClass#methodA() ");
}
}
class AnotherClass extends SomeClass {
public void methodA() {
System.out.println("AnotherClass#methodA() ");
}
}
What is the result?
Options
- AA ClassCastException is thrown at runtime.
- BAnotherClass#methodA()AnotherClass#methodA()
- CThe compilation fails.
- DSomeClass#methodA()AnotherClass#methodA()
- EAnotherClass#methodA()SomeClass#methodA()
- FSomeClass#methodA()SomeClass#methodA()
How the community answered
(41 responses)- A7% (3)
- C78% (32)
- D2% (1)
- E10% (4)
- F2% (1)
Explanation
You've hit your session limit · resets 1:50am (America/New_York)
Topics
#Inheritance#Downcasting#Type checking#Reference assignment
Community Discussion
No community discussion yet for this question.