• 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

Another question on constructors

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just bought van der Linden's book Just Java 2. There is one section about which I am totally confused. In chapter 6, starting at the bottom of p. 148, he writes the following paragraph:
As we saw in chapter 2, a superclass constructor is always invoked when you create a superclass object. If you don't explicitly call a superclass constructor, then the no-arg constructor of the superclass is called for you. If the superclass doesn't have a no-arg constructor (either an implicit one because you didn't provide any constructors or an explicit no-arg constructor that you did provide), then you will get a compilation error along the lines of "no constructor found in superclass."
Is this a misprint or am I missing something here (i.e., can some reinterpret this so that it makes sense?).
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is about constructing objects. Java makes sure that an object is constructed (new-ed) only by calling a constructor. So, when there is no constructors provided by the programmer, the compiler adds a no-arg constructor. There are two cases:
i) When the class has no explicit super class
In this case a no-arg constructor (with empty body) is added by the compiler.
ii) When the class has an explicit super class
A no-arg constructor with a call to base class's no-arg constructor is added. When the compiler finds no base class constructor (no-arg), then it objects.
 
You guys haven't done this much, have ya? I suggest you study 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