Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor

 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic