File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Instance Method/Static Method/Field Hiding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instance Method/Static Method/Field Hiding" Watch "Instance Method/Static Method/Field Hiding" New topic

Instance Method/Static Method/Field Hiding

Aruna Tirkey

Joined: Oct 12, 2008
Posts: 1

Child.public void method1()
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

Joined: Oct 13, 2005
Posts: 46368
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

Joined: Apr 15, 2008
Posts: 131
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

My Techie Blog - Java Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46368
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

Joined: Oct 27, 2005
Posts: 20275

Added code tags, and moving to beginners.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Instance Method/Static Method/Field Hiding
jQuery in Action, 3rd edition