wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Qa dd Watch "Qa dd New topic
Author

Qa dd'rouy Again

Umair Uddin
Greenhorn

Joined: Sep 12, 2001
Posts: 14
Q3

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();
t.show();
Question4 q=new Question4();
q.show();

}
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 ...
Thankx
Umair Uddin Qa dd'rouy
[This message has been edited by Cindy Glass (edited October 05, 2001).]
Michael Ernest
High Plains Drifter
Sheriff

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: t.show() 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"
Sheriff

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: http://aspose.com/file-tools
 
subject: Qa dd'rouy Again
 
Similar Threads
plz help
plz help
Tips,oneliner Just before the SCJP Exam
Question
Sub Class and super class