Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface

 
Vidhya Hari
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();}
}



Would'nt this code result in a compiler error cos it is trying to reassign a final member of the Interface I?
--------------------------------------------------------------------------------
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vidhya Hari:
...Would'nt this code result in a compiler error cos it is trying to reassign a final member of the Interface I?

Unlike methods, fields are not overridden. Instead, they can be "hidden" with levels of inheritance.

As the output of this code demonstrates, none of these variables have been reassigned. There are 4 distinct "s1" variables, and the reference type determines which variable is accessed.

See JLS, Section 8.3.3.2: Hiding of Instance Variables.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic