aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding" Watch "Overriding" New topic
Author

Overriding

vendikonda sravan
Ranch Hand

Joined: May 09, 2010
Posts: 38
Can Some buddy explain why the out put is coming as " sub 2010" for the Fallowing classes

public abstract class A
{
int i=20;
abstract void a();
public static void main(String[] args)
{
System.out.println("Hello World!");
}
public int a2(){
System.out.print("super");
return i;}
}



public class C extends A
{
int i =10 ;

public static void main(String[] args)
{
A x= new C();
System.out.print(x.i+""+x.a2());
}
public void a(){}
public int a2(){
System.out.print("sub");
return i;
}
}
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

vendikonda sravan wrote: the out put is coming as " sub 2010"


but i think it should be 10sub20............not what you said


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

phil sohar wrote:
vendikonda sravan wrote: the out put is coming as " sub 2010"


but i think it should be 10sub20............not what you said


yes you are right .........actually in this.......print statement runs from right to left
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
When the System.out command runs, it first evaluates the x.a2() expression. This
causes 'sub' to be printed. It then prints the 'i' value associated with the type of
variable 'x' (type A) followed by the return value of the x.a2() expression. The " "
is needed in the print argument to assure that the + operator adds strings rather
than integers.

Jim ... ...


BEE MBA PMP SCJP-6
vendikonda sravan
Ranch Hand

Joined: May 09, 2010
Posts: 38
Thanks jim
 
jQuery in Action, 2nd edition
 
subject: Overriding