• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is final variables are override

 
Akshayan Venkatesh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final methodes are not override by sub classes. but final variables are override .Why ?. Please explain me.


Thanks in advance.

Venkatesh
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A final variable means that the value of variable cannot be changed after it is initialized. It doesn't mean it cannot be overridden.

A final method means that the method of a class cannot be overridden by a subclass.

So that is why final variables can be overridden.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables can't be overridden. They can be shadowed or hidden.

If you have a method it is possible to alter that method in a subclass: that is overriding.
If you have a field and declare a local variable with the same name, that local variable shadows the field and hides it. See the Java Language Specification page 86. The original field remains unchanged, however, and can be found with the this. syntax.
If you have a superclass variable and a subclass variable with the same name the subclass variable hides the superclass variable. There is a little example here in the JLS. Note that in that example the hiding and hidden variables are of different type, whereas overridden methods must have the same signature and just about the same return types and Exception declarations.

See also pages 162-163 of the JLS.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well!!Mr Campbell
i saw the example it is shown that the variables of super class are hidden by sublass by changing the data type but what will happen if the variable name and the data type of variable in the super class as well as sub class is same?
will the variables be hidden than how?or the variables will be overridden?
i am bit confused on this point please if you could ellaborate and explain it will be kinda of you.
Thanks in advance.
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To my way of thinking it is just a matter of terminology. If you "widen your view" a bit, hiding, in a WAY, IS overriding a variable....but our standard definitions/terminology don't call it that.

We override methods but we hide variables. It's just a matter of terminology.

I suppose it could have some difference down in the "guts" at runtime since the runtime might have some polymorphism related decisions to make on overridden methods and that MIGHT not be the same mechanism when a variable is hidden but I am only guessing because I am in WAY over my head at this point.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it and see.See what happens.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well!!
Mr Campbell
i dont know if i am right or wrong please correct me if anything wrong.
i tried the program it is giving the output x=99
the value of x is 99 in the class Bar which is a sub class ,,,,,whereas in the super class the value of x is 3 but it is initialised inside the default constructor and default constructor is called as class is initialised ............but here the value of x is not getting reflected in subclass although in the method print() the statement System.out.println consist of this.getClass().getname() but as per i know the "this" " keyword is used to refer to super class methods " is it so?

Frankly to say i am not at all sure with what is happening over here.......why this behaviour?

Please Mr Campbel could you explain the reason........
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this change:
becomesIn the subclass (Bar), x is 99, and that is what you print out. The x in the subclass hides the x in the superclass. The altered version I have just suggested will (I hope) make things clearer for you.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, "this" doesn't call superclass members. The "super" keyword does.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic