• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance Method/Static Method/Field Hiding

 
Aruna Tirkey
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



//Output
Child.public void method1()
5
Parent.public static void method2()
=============
Can someone explain why is these 3 outputs are comming. I expected it to be:
Child.public void method1() // Object's Class Method which is same as output
10 // ObjectReference's Class variable - output different
Child.public static void method2() // ObjectReference's Class method - output different
[ October 13, 2008: Message edited by: Rob Prime ]
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

More of a beginner's question. Please find the code button; your quoted code is illegible without code tags. Start with our FAQ which has an item about overriding and hiding in.
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
method3() is not present in Child class so the method of PArent clas is called.

Now the method1 of child will be called since it is present there but the method2 will be called of PArent because method3 is instance of PArent and not Child. The static method is called of the instance and method3 instance is PArent
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you try to override anything other than a non-private instance method, you don't. You hide it. And as you see, hiding class members is a sure-fire recipe for confusion.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Added code tags, and moving to beginners.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic