It's not a secret anymore!
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
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
I agree. Here's the link:
subject: Constructor
It's not a secret anymore!