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
Author

Constructor

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\A1.java
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);
System.out.println(y.x);
}
}
the above sourcefile is saved as C:\aaj\C.java
when i try to compile it using C:\aaj>javac -sourcepath C:\ C.java 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.
rajashree.
Francisco I
Ranch Hand

Joined: Mar 27, 2001
Posts: 44
Try this in A1:
package shreejava;
public class A1{
protected int x;
A1(){}
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.
http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html


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