Menu
Topics Index
...
`

Reflection Print Simple Name


If you need explanation Read this topic

If you need Answer Take test on this topic

Reflection Print Simple Name
What will be the output of the following program?
public class Reflection {
    public static void main(String[] args) {
        A aa = new A();
        A ab = new B();
        A ac = new C();
        printDetails(aa);
        printDetails(ab);
        printDetails(ac);
    }
    private static void printDetails(Object obj) {
        System.out.println(p(obj.getClass()) + "~" + p(obj.getClass().getSuperclass()) + "~" + p(obj.getClass().getSuperclass().getSuperclass()));
    }
    private static String p(Class<? extends Object> clazz) {
        return clazz == null ? null : clazz.getSimpleName();
    }
}

class A {
    void go() { }
}
class B extends A {
    protected void go() { }
}
class C extends A {
    public void go() { }
}

A. A~Object~null
B~A~Object
C~A~Object
B. A~Object~null
B~A~Object
C~B~A
C. A~null~null
B~A~null
C~B~A
D. Some other output or Compilation Error or Runtime Error
Topic: Advanced Java Programming Concepts

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

A option is corect..

Posted by Vijay Kumar    2014-05-26 13:15:26


answer is c because class has no super classes and for classes B and C they have super classes.

Posted by Shiva Goud    2014-05-26 13:30:50


answer is d

Posted by Vinod Avulapati    2014-05-26 13:31:12


ans a. 1st functn outputs class name A , superclass of invoking object and null (bcz invoking objct s type of objct)
2nd functin prints 1st class name of func,super class A,invoking objct
3rd prints class name C,super class A,invoking objct of A.

Posted by Maheshwari Natarajan    2014-05-26 13:37:39


ans :d
class name ,R.java ,are only accepted if annotation processing is explicity requested

Posted by Ananda Kumar    2014-05-26 13:52:43


Answer : A

> we get A~Object~null becoz the class we are refering is " A" its superclass is "Object" and we get null coz their is no superclass for "Object" as Object is on the top of the hirearchy in Java
>We get B~A~Object as the class being refered is "B" its superClass is "A" and the superClass to A is "Object"
>We get C~A~Object as the class being refered is "C" its superClass is "A" and the superClass to A is "Object"

Posted by Deepak Kumar Yadav    2014-05-26 15:51:58


Congratulations Maheshwari Natarajan. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-28 14:52:57


© meritcampus 2019

All Rights Reserved.

Open In App