• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JRE corrupted

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic