If we execute run the following code, the output is 10.
interface X{
int abc = 0;
}
interface XX extends X{
int abc = 10;
}
class XTest implem开发者_运维问答ents XX
{
public static void main(String[] args)
{
System.out.println("Hello World! --> " +abc);
}
}
But as per Java, interface variables are public static final. but how am I getting 10 as output?
This code works as it should.
Your XTest class implements XX, so it gets the value of abc
from the public static final instance in that interface.
XX shadows X, so it supercedes the abc
value from X.
精彩评论