| Author |
doubt in variable in inheritance
|
Sujittt Tripathyrr
Ranch Hand
Joined: Jun 21, 2006
Posts: 96
|
|
class Super { int var=10; void display() { System.out.println("Inside Super"); } } public class Inheritance extends Super { int var=19; void display() { System.out.println("Inside Inheritance"); } public static void main(String s[]) { Super i=new Inheritance(); i.display(); System.out.println(i.var); //1 } } At the line 1 while i am printing variable "i " it is printing Super class "i" . can you please clarify my doubts. Thanks
|
 |
Neelesh Bodas
Ranch Hand
Joined: Jul 20, 2006
Posts: 107
|
|
|
because variables are statically bound (as against methods, which are dynamically bound.)
|
 |
Suhas Wadadekar
Ranch Hand
Joined: May 16, 2006
Posts: 95
|
|
There is only one rule to remember for situations like this: Nothing else other than overriden methods depend on object type. Everything else other than overriden methods depend on the type of the reference variable Hope this clears your doubt.... pls let me know and if I am wrong
|
 |
Sujittt Tripathyrr
Ranch Hand
Joined: Jun 21, 2006
Posts: 96
|
|
Thanks Suhas I cleared my doubt.
|
 |
 |
|
|
subject: doubt in variable in inheritance
|
|
|