It's not a secret anymore!
The moose likes Beginning Java and the fly likes Inheritance and static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance and static methods" Watch "Inheritance and static methods" New topic

Inheritance and static methods

sovan chatt
Ranch Hand

Joined: Aug 09, 2010
Posts: 43

The output of the code is h1 h4;so,the p1 method calls parent class's P() method and child class's P2() method.Which I feel is very confusing.What is the general concept?
Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

The only thing that is runtime polymorphic in Java is a non-private, non-static, non-final method.

In other words, if a method is non-private, non-static, and non-final, then the JVM will decide at runtime which class's implementation of that method to call, based on the runtime class of the object.

For all other methods--all methods that are either private, static, or final (or any combination)--the compiler determines which class's implementation to call, based on the compile-time type of the reference.

I agree. Here's the link:
subject: Inheritance and static methods
It's not a secret anymore!