Given: What is the result?
A.
Base DerivedB
B.
A classcast Except ion is thrown at runtime.
C.
Base DerivedA
D.
DerivedB DerivedA
E.
DerivedB DerivedB
Given: What is the result?
A.
Base DerivedB
B.
A classcast Except ion is thrown at runtime.
C.
Base DerivedA
D.
DerivedB DerivedA
E.
DerivedB DerivedB
E.
Checked with NetBeans
B is correct.
Answer is E. Checked with NetBeans.
I checked it with Eclipse and is E
Answer: E
DerivedB DerivedB
Tested
E
People, better if you write explanation WHY it is E instead of giving the same answer multiple times.
E is correct
if a non-static method is overrided by a subclass
then JAVA will use this overrided method , no mmater what Type this object is declared
but for the variable and static method , which target should be used depend on the Type
object type and reference type are different things, just look at the object creation – “new” keyword – once object is created it doesn’t change – casting it to superclass later is only changing reference type
https://coderanch.com/t/416816/java/Casting-super-class-class