Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overidden variables

 
allanctan
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why does q.x return 7 as well???
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does q.getY() return?
 
Sagar Sharma
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so q.getY() returns 5 ?
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Javaranch appreciates your cooperation to comply with the official naming policy.
Ajith
 
Allan Tan
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
q.y() returns 5. Thanks!
 
SHALINI PATEL
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic