File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding static methods" Watch "Overriding static methods" New topic

Overriding static methods

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

The following example:

Outputs the following:

I understand why the Parent's get1 method gets called on both instances i.e. because static methods are not overridden. What i don't understand is why the output is different if i override the get() method as shown below:

With the overridden get() method, the output is

Why does the version with the overridden get() method not call the Parent's get1() method for the object using the Parent reference (i.e. Parent p = new Child())?


Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

consider the below code

if you call this method on a reference which points to his sub class object then it the method is static it process the reference's method else sub class's method. and in your second case get method of sub class called[because get() is an instance method] and then it execute its own static get method[this.get1()]
I agree. Here's the link:
subject: Overriding static methods
jQuery in Action, 3rd edition