What is the result?

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?

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.



Leave a Reply 4

Your email address will not be published. Required fields are marked *


James

James

The Answer is D.

carmen

carmen

D because the function gets the class from the object referenced at runtime.

tsivasankar

tsivasankar

D is the correct answer