File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Instance Method/Static Method/Field Hiding

Aruna Tirkey
Greenhorn

Joined: Oct 12, 2008
Posts: 1



//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

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


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Campbell Ritchie
Sheriff

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

Joined: Oct 27, 2005
Posts: 19670
    
  18

Added code tags, and moving to beginners.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instance Method/Static Method/Field Hiding