Given:
public class AnotherSampleClass extends SampleClass {
public static void main(String[] args) {
Object obj = new AnotherSampleClass();
System.out.println(“sc: ” + obj.getClass().getName());
System.out.println(“asc: ” + ((AnotherSampleClass)obj).getClass().getName());
}
}
public class SampleClass {
}
What is the result?
A.
sc: java.lang.Object
asc: AnotherSampleClass
B.
sc: SampleClass
asc: AnotherSampleClass
C.
sc: AnotherSampleClass
asc: SampleClass
D.
sc: AnotherSampleClass
asc: AnotherSampleClass
Explanation:
Note: The getClass method Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.
Note: Because Java handles objects and arrays by reference, classes and array types are known as reference types.
D
The Answer is D.
D because the function gets the class from the object referenced at runtime.
D is the correct answer