File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor " Watch "constructor " New topic


swapnil paranjape
Ranch Hand

Joined: May 15, 2005
Posts: 126
please explain the output

i was expecting the output as

but output is
b = 0
b = 16

actualy subclass constructor should call super (base class constructor) but thats is not happening here..please explain

Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

I think this is what you were trying to do...

The function call is over ridden, so the super constructor calls the derived class call() function. At this point of time b is not initialized with 16 so it prints 0. I changed the function name from call to call_one in the base class.

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Flom Xanther
Ranch Hand

Joined: May 26, 2006
Posts: 48
just for your info,
if you would make call() in bc private, then the result is what you expect..

Because private methods/attributes are not inherited, so no way for polymorphism..

swapnil paranjape
Ranch Hand

Joined: May 15, 2005
Posts: 126
when the base class contructor calls the method call() its actually (am i right?) so it should invoke the base class call method. why the call method from subclass is called?

please explain.
I agree. Here's the link:
subject: constructor
It's not a secret anymore!