File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor doubt" Watch "Constructor doubt" New topic
Author

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:

test_constructor2.java:17: 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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

Sen,
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.



[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
marc weber
Sheriff

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
sscce.org
Sen George
Ranch Hand

Joined: Sep 21, 2005
Posts: 76
Thank you both for the insights. The doubt is cleared.
 
Consider Paul's rocket mass heater.
 
subject: Constructor doubt
 
Similar Threads
Constructor doubt
default constructor must inside the Parent class. why?
About System.out.println() method
Inheritance - weird behaviour ?
Pattern matching doubt