aspose file tools*
The moose likes Beginning Java and the fly likes Default Constructor in Sub/Super Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Default Constructor in Sub/Super Class" Watch "Default Constructor in Sub/Super Class" New topic
Author

Default Constructor in Sub/Super Class

Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39788
    
  28
As far as I know, the compiler looks for a no-args or default constructor, unless you write super(foo); in the constructor.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3737
    
  16
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) ...


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39788
    
  28
You are right, Joanne and Ernest; if it hadn't said "default" there would have been no confusion.
Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
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.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
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

SCJA
~Currently preparing for SCJP6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39788
    
  28
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

Joined: Aug 05, 2005
Posts: 3737
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default Constructor in Sub/Super Class