Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Qa dd'rouy Again

 
Umair Uddin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic