File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program Output " Watch "Program Output " New topic

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


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


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


B b=new A();

will go with error


A b=new B();

is equal to

A a =new A();
B b=new 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

Joined: Oct 13, 2005
Posts: 46375
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.
I agree. Here's the link:
subject: Program Output
It's not a secret anymore!