aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding static methods" Watch "Overriding static methods" New topic
Author

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())?

Thanks


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()]
 
wood burning stoves
 
subject: Overriding static methods