Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

constructor up and down hierarchy

 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Look a code



It gives compile time error.
It seems to me that compiler insert no-arg default constructor for class Cert. and don't find no-arg constructor in Marmaduke.

when a object is instantiated for subclass , then instantiation of its super class must be done first. i.e you have to complete the UP hierarchy

my doubt is :

i am creating instance of Marmaduke class . So i must ensure the instantiation of Marmaduke and Object class which is UP hierarchy.which is fine since compiler will place super() in Marmaduke fo Object class.

but why do i have to worry about the Cert i.e. DOWN hierarchy ??



Regards
naveen




 
Ranch Hand
Posts: 47
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Extending class has to define constructor, which can call constructor of extended class with corresponding argument list. Compiler puts call to super(...) constructor in every first line of your extending class constructors.
You don't define any constructor in extending class, so compiler puts default no-arg, but it cannot insert call to super() with no args, so refuses to compile such a code.
It doesn't matter you don't instantiate extended class in your code.
 
naveen yadav
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


it means compiler will check constructor for every class in parent and child class relationship , irrespective of class you are instantiating.

it starts constructor chaining right form child class to Object class. Is that it ?
 
Tomasz Sochanski
Ranch Hand
Posts: 47
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveen yadav wrote:

it means compiler will check constructor for every class in parent and child class relationship , irrespective of class you are instantiating.

it starts constructor chaining right form child class to Object class. Is that it ?


Yes, exatcly. Please notice, that you can instatiate object without using new operator - ie. you can do this dynamically with reflection API - how compiler would determine if you would or would not do this?
 
naveen yadav
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Tomasz Sochanski .
 
expectation is the root of all heartache - shakespeare. tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic