File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt with overloaded super 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 "Doubt with overloaded super constructor" Watch "Doubt with overloaded super constructor" New topic
Author

Doubt with overloaded super constructor

Geetha Gubendran
Ranch Hand

Joined: Sep 04, 2006
Posts: 72
hi,

When I tried to run the below program. I got the error as

ConstructorTest.java:18: cannot resolve symbol
symbol : method SuperConstrctr (int,int)
location: class SubConstrctr
SuperConstrctr(i,j);
^
1 error

but in the rules of constructor, I read that

==========================================================================
A call to super() can be either a no-arg call or can include arguments passed to the super constructor.
==========================================================================

Can somebody help to figure out the problem with the below program.
class SuperConstrctr
{
int k,l;
SuperConstrctr() {
System.out.println("No arg super constructor");
}
SuperConstrctr(int i,int j) {
this.k = i;
this.l = j;
System.out.println("Super arg Constructor");
}
};

class SubConstrctr extends SuperConstrctr
{
int i,j;
SubConstrctr(int i,int j) {
SuperConstrctr(i,j);
this.i = i;
this.j = j;

System.out.println("Subclass Constructor");
}
};
public class ConstructorTest
{

public static void main(String[] args)
{
SubConstrctr constrctr = new SubConstrctr(30,20);
}
}

Thank You.
Charmy Madhvani
Ranch Hand

Joined: Dec 26, 2007
Posts: 59
class SuperConstrctr
{
int k,l;
SuperConstrctr() {
System.out.println("No arg super constructor");
}
SuperConstrctr(int i,int j) {
this.k = i;
this.l = j;
System.out.println("Super arg Constructor");
}
};

class SubConstrctr extends SuperConstrctr
{
int i,j;
SubConstrctr(){}
SubConstrctr(int i,int j) {
super(i,j);//Your code is changed here. You have to call super constructor using key word super.
this.i = i;
this.j = j;
System.out.println("Subclass Constructor");
}
};
public class ConstructorTest
{
public static void main(String[] args)
{
SubConstrctr constrctr = new SubConstrctr(30,20);
}
}


SCJP 5.0<br />SCWCD 5.0
Ernesto Elias-Nieland
Ranch Hand

Joined: Sep 01, 2005
Posts: 53

You call a super constructor by: super(i,j). This must be your first line in the SubContructor constructor not SuperConstr(i,j).

Cheers,
Ernie


0x2B | ~0x2B
SCJD, SCJP 1.5, SCJP 1.4, SCBCD 1.3, SCWCD 1.4, SCJA, IBM Test 150, 142, 700, 701, Oracle Test 1Z0-51
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt with overloaded super constructor