Java Interface Request

If we run the following code, the output will be 10.


interface X{ int abc = 0; } interface XX extends X{ int abc = 10; } class XTest implements XX { public static void main(String[] args) { System.out.println("Hello World! --> " +abc); } } 

But according to Java, interface variables are publicly available. but how do I get 10 as output?

+4
source share
1 answer

This code works as it should.

Your XTest class implements XX, so it gets the value abc from the open static final instance in this interface.

XX is the shadow of X, so it cancels the value of abc from X.

+5
source

All Articles