• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on Constructors- from K&B book

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everybody

Given the following

1. Public class Test{
2. public static void main(String [] args){
3. Parent p=new Child();
4. }
5. }
6.
7. class Parent{
8.public Parent(){
9. //
10. System.out.print("pc");
11. }
12. }
13.
14. class Child extends Parent{
15. public Child(){
16. System.out.print("cc");
17. }
18. }

what is the result?

A pc
B cc
C pc cc
D pp cc
E Compilation Fails
F An Exception is raised

I chose C ,but according to K&B explanation the correct answer is D

Can anybody explains why D is Correct.

Thanks
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried to compile it?
The answer is c. it has nowhere it could have printed pp.
Did you type the program correctly?
The way it is it print pccc
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually k&b are right.
we are providing explicit constructor. hence we have explicitly give call to super().. hence compilation error

correct me if i m wrong
 
Tamara Lopez
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compilation wont fail
you are right - it will call super() - which is the parent constructor and print pc then go to the child constructor and print cc
try it out
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sushma,
There wont be any compiler error as indicated in some replies. This is because the child class constuctor will implicitly call the base call constructor using super(). So v have no need to call the base class constructor using super(). Now first base class constructor will be executed hence pc will be printed followed by derived class constructor hence cc will be printed.
Sushma, u can point out this mistake to the authors k & b.
 
sushma yeruva
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Thanks Everybody
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic