File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Constructor doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor doubt" Watch "Constructor doubt" New topic

Constructor doubt

Sen George
Ranch Hand

Joined: Sep 21, 2005
Posts: 76
I have the following two pieces of code.

First piece of code

The above code compiles and runs successfully.

Secod piece of code

When compiled, the above code give the following error: cannot find symbol
symbol : constructor Parent()
location: class Parent
public test_constructor2(String s) {out.println (" \n test_constructor2 constructor is running .... "); }
1 error

But if the No-Arg constructor of class Parent is enabled, the code compiles fine.

I am just not able to reason out the difference in this behaviour. Could someone please clarify this?

[ edited to add code tags to second block ]
[ February 06, 2008: Message edited by: Jeanne Boyarsky ]
Sen George
Ranch Hand

Joined: Sep 21, 2005
Posts: 76
I failed to clarify my first question. Why is it that a No-Argconstructor is not enforced in the first piece of code but enforced in the second?
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33124

The first example has a default constructor. If you don't type any constructors in a class, Java provides one without any arguments automatically. If you type in a constructor of your own, Java doesn't add a default one.

Note that in the second example, you could use "super" to force the provided constructor to be called. This would prevent the compiler from looking for a non arguments constructor.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
marc weber

Joined: Aug 31, 2004
Posts: 11343

Unless a constructor contains an explicit call to "super" or "this" as its first line, there is an implicit call to super with no arguments. So in both of your examples, the subclass constructor is implicitly calling super() with no arguments.

If no constructors are defined for a class, the compiler automatically inserts a default no-args constructor.

In your first code example, there are no superclass constructors defined, so the compiler inserts a no-args constructor. Then when the subclass calls super(), it works fine. But in your second code example, your superclass has a constructor defined that takes a String argument, so the compiler does not automatically insert a no-args constructor. Therefore, the subclass call to super() will fail unless you explicitly define a no-args constructor in the superclass.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Sen George
Ranch Hand

Joined: Sep 21, 2005
Posts: 76
Thank you both for the insights. The doubt is cleared.
I agree. Here's the link:
subject: Constructor doubt
jQuery in Action, 3rd edition