Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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

what is the usage of super() to invoke super class constructor!

 
Ranch Hand
Posts: 118
Android Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi



when the above program is executed first SuperClass Constructor and then ChildClass constructor is (executed)displayed. What is the use of having the super keyword in the child class constructor when infact the super class constructor is executed when the object for the child class is created since the child class extends to the superclass.

Thanks
 
Saloon Keeper
Posts: 13878
314
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no real use to call super(), because it's called implicitly whether you write it or not. Some people prefer to make it explicit, but it's unnecessary.

However, it *is* important to call super(...) at the start of the constructor if you want to invoke a superclass constructor that has parameters. Here's an example:
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks stephan,

So unless we have to call the constructor which has got parameters it's ok not to use super keyword as this is implicitly called.. correct me if am wrong...
 
Stephan van Hulst
Saloon Keeper
Posts: 13878
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that's correct.
 
Bartender
Posts: 1249
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • Let creation of an object of subclass invokes non-parameterized Or parameterized constructor of subclass, implicitly It invokes non-parameterized constructor of superclass by default. Sometime you may need to invoke a particular type of superclass's constructor rather than non-parameteried as given in Stephan's example then you can use super keyword.
  • That super keyword statement should always be first statement in constructor of subclass.
  •  
    machines help you to do more, but experience less. Experience this tiny ad:
    Free, earth friendly heat - from the CodeRanch trailboss
    https://www.kickstarter.com/projects/paulwheaton/free-heat
    reply
      Bookmark Topic Watch Topic
    • New Topic