Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Overriding Interface constant in an implementing class.

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

So its been 4 long years ever since I'm not working in Java ( all these years i was in PHP/Perl/Python and Cold fusion and I'm an SCJP). I was planning to shift to Java again and started reading my old certification book this morning.

I'm stuck at one point.

It is correct that interfaces define constants. But can a class which implements that interface override (or shadow) that variable?

in other words will the below code execute without any error?

And what if I want the values of both the variables in the above code without changing the variable name?

Thank You,
Abhijit.
(I know being an SCJP I should know this, but sorry. I'm completely out of touch )

 
Sheriff
Posts: 22509
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the interface name: Go.BAR is the constant* from the interface, and this.BAR or BAR is the instance field from the class.

* fields in interfaces are always public, static and final.
 
Saloon Keeper
Posts: 13394
296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you tried compiling that code?
 
Abhijit Ghatnekar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Rob: Thanks, man. after struggling through the code for sometime, I managed to echo out two different values of that constant.. :-).
@Stephan: That code compiled but I was amazed as to how it did and I wanted to confirm whether it is shadowing or it is something else :-)

This forum rocks and of course Java does too.

Cheers,
Abhijit
 
Rob Spoor
Sheriff
Posts: 22509
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhijit Ghatnekar wrote:@Rob: Thanks, man. after struggling through the code for sometime, I managed to echo out two different values of that constant.. :-).


There are no different values of any constant. That beats the purpose of a constant, doesn't it?
What you meant is that you got one value for the constant and a different value for the instance field.

And you're welcome
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic