Forums Register Login

Doubt with overloaded super constructor

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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);
}
}
+Pie Number of slices to send: Send
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
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 758 times.
Similar Threads
Overload constructor
A simple Q but still have doubt
Constructor
doubt regarding constructors
Programmer's Final Exam question #13 (from imon Robert's book)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:24:36.