wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Polymorphic Madness! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Polymorphic Madness!" Watch "Polymorphic Madness!" New topic
Author

Polymorphic Madness!

Rosie Nelson
Ranch Hand

Joined: Nov 06, 2001
Posts: 31

OK,
Static methods are not overridden, they are hidden. The aMethod() in the subclass (Sub) "shadows" the one in the superclass (Base).
Now, when I ran the above code i got:
x Value in Base:5;
So obviously you don't get the polymorphic behaviour one would expect with overriding if you hide a static method. The underlying object at runtime is a Sub but yet the aMethod() of Base is invoked?

My question is this:
What determines the choice of method that is invoked in this situation. Can we generalise and say that the type of the reference variable b determines which method will be invoked?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

Originally posted by Rosie Nelson:
My question is this:
What determines the choice of method that is invoked in this situation. Can we generalise and say that the type of the reference variable b detrmines which method will be invoked?

For static methods, the declared type of the reference variable determines which method will be invoked. b is declared as being a Base type, so Base.aMethod() is invoked.
Junilu


Junilu - [How to Ask Questions] [How to Answer Questions]
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Also, think about static binding and Late binding..
static binding done in compiler time and late binding is done on runtime.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

static binding done in compiler time and late binding is done on runtime.

Not for our loved Java. Because the compiler places symbolic references to represent methods invocations. These references are made up of the names of the class containning the method, the descriptor of the method and the name of the method. When the "interpreter" resolves a class replaces these references for the actual address in memory. So all methods in Java are bound at runtime.
We can associate static binding to the one that is decided based on the declared type of the reference. And dynamic based on the type of the object pointed to by the reference.

SCJP2. Please Indent your code using UBB Code
Rosie Nelson
Ranch Hand

Joined: Nov 06, 2001
Posts: 31
Thank you Bartender.. have a drink on me Thanks Fei and Jose for your responses.
One last thing to clear up re: private method invocation.
JLS (8.4.6.3 Requirements in Overriding and Hiding) states the following:-

Note that a private method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass.

Question: -
What is the mechanism for private method invocation i.e. when are private methods resolved, compile time or runtime? i.e. does a private method partake in dynamic binding or static binding?

Here are twO conflicting views I've seen on this Bulletin Board ... Which is correct?

View 1

"Private methods are resolved at runtime, so there IS polymorphism going on. Only what you don't see is that the JVM is keeping track of the "owner" of the method. When you "re-declare" a method in the sub-class the compiler knows that you will not be overriding the parents private class so in the backgound it creates the method and tags it with a different internal name to keep them separate. So you can USE it, it is just not an example of overriding."
(This is kinda what Jose is saying)

View 2
"We usually use the term overriding in context of polymorphism. Private methods do not participate in this in any way."
[This message has been edited by Rosie Nelson (edited December 02, 2001).]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
private methods are resolved statically. This is at runtime, wihtout polymorphism and based on the declared type of the reference.
Rosie Nelson
Ranch Hand

Joined: Nov 06, 2001
Posts: 31
It is my understanding that static binding is done at compile time, so the method to be invoked is determined by the type of reference variable rather than the actual type of the object it holds at runtime.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
to Rossie
Decode a class file and you will see that all method invocations are symbolic references in the constant pool of the class. When the JVM resolves the class (at runtime) replaces these symbolic references for direct ones. The direct ones are pointers to the addresses where static methods habit, or indexes to the method table if a non static bound is performed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Polymorphic Madness!
 
Similar Threads
Overloading
Calling Overridden methods and overshadowed variables
Calling Overridden methods and overshadowed variables
Inheritance/Polymorphism question
ststic variables