| Author |
Interface variables
|
Kathy Hodgson
Greenhorn
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? Thanks, Kathy
|
 |
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.
|
 |
 |
|
|
subject: Interface variables
|
|
|