user421147
user421147

Reputation:

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 implements 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?

Upvotes: 5

Views: 343

Answers (1)

duffymo
duffymo

Reputation: 308753

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.

Upvotes: 6

Related Questions