• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Axes Axes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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?
 
karthikeyan Chockalingam
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • 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.

 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • 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?
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • 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
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • 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".
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To restate what has already been said with other words: fields aren't polymorphic.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!