开发者

Java Interface Query

开发者 https://www.devze.com 2023-01-11 10:43 出处:网络
If we execute run the following code, the output is 10. interface X{ int abc = 0; } interface XX extends X{

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

精彩评论

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