This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
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
Joined: Mar 17, 2000
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