wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overidden variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overidden variables" Watch "Overidden variables" New topic
Author

Overidden variables

allanctan
Greenhorn

Joined: Oct 30, 2000
Posts: 1
Why does q.y returns 7?

[I added UBB CODE tags to your source code to make it more readable. Please try to use
them in the future. Learn more about UBB codes here - Ajith ]

[This message has been edited by Ajith Kallambella (edited October 31, 2000).]
Brian, Cavanagh
Ranch Hand

Joined: Oct 30, 2000
Posts: 70
why does q.x return 7 as well???
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

what does q.getY() return?


SCJP
Visit my download page
Sagar Sharma
Ranch Hand

Joined: Aug 31, 2000
Posts: 92
hi,
Method calls are always determined by the state of the object at runtime, but the variables are referenced using the reference of the object.
Since object q has a reference variable of type Shape q.y will always give 7. Same is the case for q.x.
Hope this helps

sagar
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

so q.getY() returns 5 ?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Javaranch appreciates your cooperation to comply with the official naming policy.
Ajith

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Allan Tan
Greenhorn

Joined: Oct 31, 2000
Posts: 7
q.y() returns 5. Thanks!
SHALINI PATEL
Ranch Hand

Joined: Oct 31, 2000
Posts: 41
I think u r referring to variable and method shadowing.
When u invoke a method by an object it is a class of the current object and not the type of reference.
example...
//Tubelight extends light and has a method called getBill()
Light l1 = new Tubelight()
Hence if yu want to excess getBill yu just say
li.getBill();
But the variable accessed is from the class corresponding to the reference type regardless of the object denoted by reference.
example.
//If both Light and Tubelight class has an instance variable int x1
Light l1 = new Tubelight()
Then if yu invoke l1.x1 then its the instance variable of light class that is invoked.
I hope yu get it. Hope somebody can correct me if I am wrong.
shalini
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
shalini you are right except for the terminology.
Instance methods can be overridden by subclasses.
Static methods gets shadowed or hidden when redefined by the subclass(es). Overriding does not apply to static methods.
Variables( static or instance ) gets hidden or shadowed when redeclared by subclass(es) because the name resolution starts from the immediate enclosing scope.
Just my two cents worth....
Ajith
 
GeeCON Prague 2014
 
subject: Overidden variables