Get your CodeRanch badge!*
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 Java 8 in Action this week in the Java 8 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: 36590
    
  16
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: http://aspose.com/file-tools
 
subject: Program Output
 
Similar Threads
Overriding and redefining
regarding overloading-Dan Chisholm's Mock Exam question
mock01a Q36
Question on therad q.17 pg 756
instance member access.. help me