This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
i compiled and run this simple programm and got strange output. pls check ************************* //Downcasting of object reference class sup { int supvar=5; protected int meth() { System.out.println("Super class method"); return 1; } } class k27 extends sup { public int meth() { System.out.println("Sublcass method(overridden) called"); return 2; } public static void main(String args[]) { k27 sub=new k27(),sub2; sup sobj=new sup(),sobj2; sobj2=sub; sub2=(k27)sobj2; System.out.println("superclass var invoked via super reference: "+sobj2.supvar); System.out.println("sobj2.meth()"+ (sobj2.meth())); System.out.println("sub2.meth()"+ (sub2.meth())); } } ********************* the out put of this program at my computer is : superclass var invoked via super refernce: 5 subclass method(overridden) called sobj.meth()2 subclass method(overridden) called sobj.meth()2 I am unable to understand why the string "sobj.meth()" doesn't prints before the print stmt. in the method .
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
You were expecting it to print the sobj2.meth() String BEFORE it knows WHAT to print? Before it knows the outcome of the call to the method? That would be fairly amazing. Expressions and statements are evaluated and resolved before executing.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Rashmi Hosalli
Ranch Hand
Joined: Jun 25, 2001
Posts: 50
posted
0
Cindy, I agree with you...but I wanted to know why the return value appears in the end after both the statements are printed out.Could you please explain? Thanks, Rashmi
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Hi Rashmi, The sub2.meth() call has to be evaluated to see what needs to be concatenated to "sub2.meth()". The string gets built and then printed. It doesn't print the first part, evaluate and print the second part. Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform