This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread 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 Android Security Essentials Live Lessons this week in the Android forum!
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: 38033
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.
It is sorta covered in the JavaRanch Style Guide.
subject: Program Output
Similar Threads
instance member access.. help me
Question on therad q.17 pg 756
mock01a Q36
Overriding and redefining
regarding overloading-Dan Chisholm's Mock Exam question