Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Qa dd'rouy Again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Qa dd Watch "Qa dd New topic

Qa dd'rouy Again

Umair Uddin

Joined: Sep 12, 2001
Posts: 14

as you know the answer is
Show method in Test Class
Show method in Question Class
but if i will change little bit code in main as ....
public static void main(String args[]) {
Test4 t=new Question4();;
Question4 q=new Question4();;

answer is same ..i am so confuse b/c i learn as in polymorphism...
JVM consider object type not Refernce type .. so why output is not
Show method in Question Class
Show method in Question Class
i think it's because of static context .. please give me the description...
as you can ( go indepth of concepts )what's the reason behind all ...??
please please ...
Umair Uddin Qa dd'rouy
[This message has been edited by Cindy Glass (edited October 05, 2001).]
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

Polymorphism is just a concept, not an "implementation detail" of Java; the concrete behavior you're looking for is more properly called "run-time" binding. Run-time binding makes it possible to address an object in memory by any valid reference type, and still get the "local" behavior of the object itself. This assumes, of course, that the same behavior is accessible from both the compile-time reference and the run-time object.
Now if a method is declared static, its behavior belongs to the immediate class. It then no longer matters what reference type you point to: will always be a class-level invocation.
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
The methods show () that you create are static. If they were instance methods, then the "polymorphism" that you are referring to would work as you expect it. Take the static keyword away from the methods and it will work. This is probably not a full explanation, someone else help please?

Michael J Bruesch<br /><i>I code, therefore I am.</i>
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
I think that you explained it well.
In order for the statement:
Test4 t=new Question4();
to print "Show method in Question Class" there would have to be some late binding going on so that the Question4 method could get picked up in preference over the Test4 method of the variable type that t is. There is no late binding involved with variables or static stuff, so the static method of Test4 gets called - cuz that is the type that the variable is.

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: Qa dd'rouy Again
It's not a secret anymore!