• 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 Chaining

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

Following lines are from K&B book,page no.128.(Constructor Chaining)


We know that constructors are invoked at runtime when you say new on some class
type as follows:
Horse h = new Horse();
But what really happens when you say new Horse() ?
(Assume Horse extends Animal and Animal extends Object.)
1. Horse constructor is invoked. Every constructor invokes the constructor
of its superclass with an (implicit) call to super(), unless the constructor
invokes an overloaded constructor of the same class (more on that in a
minute).
2. Animal constructor is invoked (Animal is the superclass of Horse).
3. Object constructor is invoked (Object is the ultimate superclass of all
classes, so class Animal extends Object even though you don't actually
type "extends Object" into the Animal class declaration. It's implicit.) At
this point we're on the top of the stack.
4. Object instance variables are given their explicit values



Animal class constructor is implicitly called when we give a statement like:
Horse h=new Horse();


Animal class constructor in return will call Object class constructor which is its default parent class,means Object class constructor will implicity called by Animal class construtor.
Then why point 3 and 4(above mentioned) says that Object constructor is invoked and Object instance variables are given their explicit values?we are not passing any value to Object class constructor from Horse class or Animal class.Explicit values are those which we pass to a constructor.But here which statement is passing explicit values?
Please clear my doubt.Thanks in Advance.
Priya
-------
SCJP(preparing).
 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object class does not contain any parametrized constructor
So the only constructor you can call is the no-arg one so there is no question of passing any values to it

Object instance variables are given their explicit values.


I am not sure what object that sentence refers to but Object class does not contain any instance variables
it may be possible that the 'object' in that sentence may refer to some different object ( not to the Object classes object )
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rancher

When chaining starts from a class to Object . Values of some variables
are assigned by JVM eg. hashcode .

Value of hashCode is assigned by some its algorithm .
Same way value so Class object is assigned by JVM explicitly .

I think these are kind of explicit assignments .
 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic