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

JRE corrupted

 
rahul kumar
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rashmi Hosalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic