Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor doubt

 
Sen George
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sen George
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for the insights. The doubt is cleared.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic