开发者

Java Interface Query

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜