File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface variables" Watch "Interface variables" New topic

Interface variables

Kathy Hodgson

Joined: Jun 16, 2003
Posts: 28
This code is from one of Dan Chisolm's single topic practice exams:
interface I {String s1 = "I";}
class A implements I {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {
String s1 = "C";
void printIt() {
System.out.print(((A)this).s1 + ((B)this).s1 +
((C)this).s1 + ((I)this).s1);
public static void main (String[] args) {
new C().printIt();
The correct output is given as ABCI, and that's what I get when I run it -- but I had thought it wouldn't even compile because the interface variable s1 is implicitly final and shouldn't be able to be reassigned. What am I missing here?
Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
S1 is not being reasigned, it is being hidden.
You cannot override a var. When you are casting the object types, you are actually accessing the version of the var that goes with that object type.
I agree. Here's the link:
subject: Interface variables
It's not a secret anymore!