This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor" Watch "Constructor" New topic


rajashree ghatak
Ranch Hand

Joined: Mar 10, 2001
Posts: 151
Hi Friends,

The following is the code:
package shreejava;
public class A1{
protected int x;
A1(int q){ x=q;}
the above sourcefile is saved as C:\shreejava\
package aaj;
import shreejava.*;
public class C extends A1 {

C(int j){super(j);}
public static void main(String arf[]){
C y = new C(10);
the above sourcefile is saved as C:\aaj\
when i try to compile it using C:\aaj>javac -sourcepath C:\ command,I am getting a compile error "No Constructor matching A1(int) found in class shreejava.A1"
what is wrong in this code?
thanx in advance.
Francisco I
Ranch Hand

Joined: Mar 27, 2001
Posts: 44
Try this in A1:
package shreejava;
public class A1{
protected int x;
A1(int q){ x=q;}
That should take care of it. Someone correct me if I am wrong, but when you extend a superclass, you need AT LEAST the constructor with no parameters for that superclass.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Because the constructor in A1 has no access level indicated it gets package level access. This means that only classes in the same package can access that constructor.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
subject: Constructor