Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor " Watch "constructor " New topic
Author

constructor

swapnil paranjape
Ranch Hand

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

i was expecting the output as
a=10
b=16

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
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

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..

regards,
Flom
swapnil paranjape
Ranch Hand

Joined: May 15, 2005
Posts: 126
when the base class contructor calls the method call() its actually this.call() (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: http://aspose.com/file-tools
 
subject: constructor
 
Similar Threads
Q on Method Invocation !!
Which method is called????
In Consturctor, calling Static method doubt
doubt
Constructor call