aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding member fields in interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding member fields in interfaces" Watch "overriding member fields in interfaces" New topic
Author

overriding member fields in interfaces

Michael Wiezik
Ranch Hand

Joined: Mar 19, 2004
Posts: 70
Suppose I have this bit of code:


interface I1{
String s1 = "It is implicitly final...";
}

interface I2 extends I1 {
String s1 = "...so why can I override it?";
}

Could anybody give formal explanation why can I override interface member fields, although they are final?


SCJP 1.4 - 88%
SCBCD 5.0 - 90%
SCEA - 81%
Chandra Vadlamani
Ranch Hand

Joined: Feb 15, 2003
Posts: 31
Over riding is not applicable to member variables unless they are methods. Members which are either references to primitives or objects are being shadowed and in order to access I1 related members you have to do I1.member and same with I2. With methods it is a different story. Both at run time and compile time reference is the one that decides which variable will be used.

Hope that clears some confusion.


Cheers<br /> <br />What's with the bottom line.<br />_ _ ____________________ _ _<br />SCJP 1.4, SCBCD 1.3, Prepping for SCEA.....
Praveen Singh
Greenhorn

Joined: Aug 18, 2001
Posts: 14
You can always define a new variable with the same name either in a class that implements an interface or in a interface that extends an interface,

it wiil be valid to write something :

interface I1{
String s1 = "It is implicitly final...";
}

interface I2 extends I1 {
String s1 = "...so why can I override it?";
// Here u r defining a new variable s1, valid
}

but following will give error :

interface I1{
String s1 = "It is implicitly final...";
}

interface I2 extends I1 {
s1 = "...so why can I override it?";
// Here u r trying to change the value of s1 that final and is already defined in super interface.
}

Hope this clears..


IBM Certified WebSphere Application Server System Admin<br />IBM Certified WebSphere Portal Server System Admin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding member fields in interfaces
 
Similar Threads
Inheritence
instanceof Q? for SCJP
Null String Concat
Interface Question
interface