• 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

Default Constructor in Sub/Super Class

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I told another programmer that GBTest1 would fail to compile because "Cannot find symbol : SuperClass()". It does compile and prints "SuperClass".

How could I get it wrong ? Please read this :

"If the superclass provides non-default constructors only (ie. does not have a default constructor) then this has implications for its subclasses.
A subclass that relies on its implicit default constructor will fail to compile.
This is because the implicit default constructor of the subclass will attempt to call the
non-existent default constructor of the superclass.
Any constructor in a subclass must explicitly use the super() call, with the appropriate
arguments, to invoke a non-default constructor in the superclass.
This is because the constructor in the subclass cannot rely on an implicit super() call to the
default constructor in the superclass"
Mughal and Rasmussen "A programmer's Guide to Java Certification" page 249.

Although SuperClass has a no-arg constructor it does not have a default constructor
which is what SubClass's default constructor is looking for.
I expected I needed to code a SubClass no-arg constructor with super() ; like GBTest2 :


Have I misunderstood M&R and how ?
I have some notes from the time I first read their book which mark GBTest1 with the compile error so I must have tested it out. I don't understand - very annoying.

Thank you.

 
Marshal
Posts: 79412
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know, the compiler looks for a no-args or default constructor, unless you write super(foo); in the constructor.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The language you cite is not very precise, and your interpretation hinges on a very strict reading. It's true that the technical definition of "default constructor" only applies to a constructor inserted by the compiler in a class that otherwise has none. The authors should not be using this term in the below, for the most part. It should be replaced with "no-argument constructor."

In any case, the rule is that if you don't call super() in a constructor, then the compiler inserts a call to the superclass's no-argument constructor. If that constructor does not exist, or is inaccessible, then the subclass won't compile. But if it exists, it doesn't matter whether it's a true default constructor inserted by the compiler, or simply a no-argument constructor defined explicitly in the class (as here) -- the subclass will compile in either case. The Java compiler actually has no idea whether the superclass's no-argument constructor is explicit or synthetic; all it knows is that it's there, or it's not.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Graeme Byers wrote:Have I misunderstood M&R and how ?



I think it's more a case that M&R is a little misleading. They seem to be using the term 'default constructor' to mean both an implicit (added by the compiler) no-arg constructor and an explicit (added by the programmer) no-arg constructor. In my book, the term applies only to the implicit constructor.

Graeme Byers wrote:"If the superclass provides non-default constructors only (ie. does not have a default constructor) then this has implications for its subclasses.
A subclass that relies on its implicit default constructor will fail to compile.



I would rewrite this to say

If the superclass provides non-default constructors only (and there is no explicit no-arg constructor) ...

 
Campbell Ritchie
Marshal
Posts: 79412
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, Joanne and Ernest; if it hadn't said "default" there would have been no confusion.
 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your help.

M&R have pretensions to exactitude.
For me a default constructor is the one Java makes (by default) for a class if you do not code any constructor.
A default constructor is a no-arg constructor - obviously Java cannot guess your arguments.
You can explicitly code a no-arg constructor.
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very interesting... I always thought of default constructors and no-arg constructors exactly the same. They both must call their super class constructor so I figured they were inherantly the same.

Shows what I know
 
Campbell Ritchie
Marshal
Posts: 79412
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite. A default constructor acts like thisbut a no-arguments constructor might look like thisRemember: default constructors are empty, no-args constructors might contain "real" statements or initialisations.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And of course a default constructor will contain an implicit call to its parent's no-arg constructor, whereas an explicit no-arg constructor can call any of its parent's constructors.
 
Your buns are mine! But you can have 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