Menu
Question Index
...

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~Object~null
B~A~Object
C~A~Object
A~Object~null
B~A~Object
C~B~A
A~null~null
B~A~null
C~B~A
Some other output or Compilation Error or Runtime Error

Doubts

Problems

Topic: Advanced Java Programming Concepts

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App