This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Program Output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program Output " Watch "Program Output " New topic
Author

Program Output

G. Graz
Ranch Hand

Joined: Oct 23, 2006
Posts: 30
Ok, new to Java so please go easy on me ! I can not figure what the output of the follow code is ? Can anyone help me ? Thank you so much in advance !!



Also form that would this be a legal statement ?

B b2 = new A () ;

Thanks again everyone !!
Santhi Bharath
Ranch Hand

Joined: Jun 03, 2008
Posts: 75
when you are creating an object of subclass,

there is implicit super() {which calls super class constructor }
statement prior to all the statements of the subclass constructor
so the output should be

B.f()
B.g()
A.h()
B.f()
B.g()

here overridden methods of B will execute i.e f and g and then h of A will execute and then constructor remaining statements i.e call to f and g will execute.

if you specify static qualifier for the methods then

A.f()
A.g()
A.h()
B.f()
B.g()


finally we can assign sub class object to super class object.but, we can't assign super class object to sub class object.


so,

B b=new A();

will go with error

but,

A b=new B();

is equal to

A a =new A();
B b=new B();
a=b;


thanks and regards<br />Santhi Bharath<br />SCJP 5.0, SCWCD 5.0
G. Graz
Ranch Hand

Joined: Oct 23, 2006
Posts: 30
ok, great !! I am understanding it a bit more ! Thank you again !
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Although you have correctly worked out how your class is running, don't call public methods from a constructor in "real life." Either call private methods or public final methods.
If you did anything peculiar in the overridden methods you could introduce errors into the superclass which can be very difficult to find.
 
jQuery in Action, 2nd edition
 
subject: Program Output