Joker :
When following code is run it prints "X.Q"
instead of "A<T>.X.Q"
as required by the language specification.
class A<T> {
static class X {
static class Q {
public static void main() {
System.out.println("A<T>.X.Q");
}
}
}
}
class B extends A<B.Y.Q> {
static class Y extends X {
} // X here is inherited from A
}
class X {
static class Q {
public static void main() {
System.out.println("X.Q");
}
}
}
public class Test {
public static void main(String[] args) {
B.Y.Q.main();
}
}
Can some one help me understand what will be the output of this program, as per my understanding it should be "A<T>.X.Q"
instead of "X.Q"
, Please correct me if i am mistaken some where
dasblinkenlight :
The reason that you are getting "X.Q"
printed is that X
refers to the class X
scoped to the unnamed package, not A<T>.X
. It does not matter that "outside" X
is declared after B
, because Java compiler sees it before resolving the name of B.Y
's base class.
You can force inheritance from A.X
in your code as follows:
class B extends A<B.Y.Q> {
static class Y extends A.X {
} // ^^
// Add this
}
Edit: (thanks user695022 for the comment)
Curiously, the problem goes away if the outer class is not generic:
class A {
static class X {
static class Q {
public static void main() {
System.out.println("A<T>.X.Q");
}
}
}
}
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=436818&siteId=1