• 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

Constructor problem

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
can anyone tell me why the following does not work......
class A
{
A(int w){
System.out.println(w);
}
}
class B extends A
{
B(int w){
System.out.println(w);
}
}
i get a message saying no default constructor found in class A
???
regards
pranav

 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should help you:
http://enthuware.com/jqplus/FAJQuestions.html#DEFAULTCONSTRUCTOR
-Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thing that your constructor in B does is try to call the no-args constructor in the super class A. But this does not exist. The compiler will automatically provide a default constructor if you have not specified any other constructors.
You can fix your code by explicitly calling the correct super class constructor or by providing a no-args constructor in A.
<pre><code>
class A
{
A(){} // no-args constructor with null implementation
A(int w){
System.out.println(w);
}
}
class B extends A
{
B(int w){
super(w); // invoke superclass constructor
System.out.println(w);
}
}
</pre></code>
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
To clarify your doubt, lemme cut-paste a clause from JLS that explains clearly what happens:
"If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body is implicitly assumed by the compiler to begin with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments."
So, in your code,
class B(int w)
{
//super(); This is included automatically!
...
}
Since you have provided a constructor for A, the default constructor for A is not considered and hence the error! To rectify this, either include a default constructor for A of type
A()
{}

or
in your class B constructor, include a line to say,
B(int w)
{
super(w);
...
}
Then, it would work fine.
Aparna
 
Pranav Jaidka
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot guys its clear now
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic