Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interface" Watch "interface" New topic
Author

interface

Vidhya Hari
Greenhorn

Joined: Mar 20, 2006
Posts: 20
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: interface
 
Similar Threads
Interface Question
Object orientation
Q from Dan's exam
interface rule
Doubt in an Interface Question!!