• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can we redefine a final variable of the interface in a class

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface Tes
{
int VAL=30;
}
class Test implements Tes
{
int VAL=40;
}

when i try to redefine a final variable of the interface in a class
it allows me to do. Can we redefine it.("dont wrongly interpret as reassign") if yes then my "final" concept is been affected. Any programmer can change the final variable by redefining in the class as they implement?
 
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can redefine it in a subclass that extends a class or in a class that implements an interface. final is here to say that the value will be initialised once and from then on treated as a constant.

Even the below code will not give error where you define a variable local to a method.

 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think "redefine" is the proper term, at least I don't think that word is used in the JLS. And do you realise you can do the following?
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'd say that the second declaration "shadows" the first one. more or less the same as if you use two different names...

jan
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice that this has NOTHING to do with the concept of final. The variable that is declared in the class is a DIFFERENT variable, even though it has the same name. So when you assign a value to it, it does not change the value of the variable in the interface.

Layne
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You aren't redefining anything, Test.VAL and Tes.VAL are two different variables. In this case one will hide the other which may give the erroneous impression that it's overridden it. As Layne mentioned this has nothing to do with the keyword "final".
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To restate what has already been said with other words: fields aren't polymorphic.
reply
    Bookmark Topic Watch Topic
  • New Topic