• 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

constructor

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the out of the following program?

....and why?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. In main() Dolphin constructor is invoked.
2. Before "Dolphin" is printed, JVM implicitly inserts super(), which calls Mammal constructor.
3. In Mammal constructor, before "Mammal" is printed super() calls Object constructor.(you are on top of the stack).
4. Object instance variables are instantiated, if any?
5. Mammal instance variables are instantiated, �Mammal� printed.
6. Dolphin instance variables are instantiated, �Dolphin� printed.
cheers
[ July 07, 2003: Message edited by: Alex Radomski ]
[ July 07, 2003: Message edited by: Alex Radomski ]
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First Mammal is output and then Dolphin.
The reason being that the first line in any consrtuctor, even if it is not explicitly written, is a no-ard call to super. Thus when you create your dolphin instance within its constructor it placed a call to super() thus executing the mammal constructor. And since all objects extend from class Object, within the mammal constructor, the first line is an implicit call to the no-arg constructor of the Object super class.
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, it looks like someone responded as I was responding, sorry for the rehash of the orig answer.
reply
    Bookmark Topic Watch Topic
  • New Topic