Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt with overloaded super constructor

 
Geetha Gubendran
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
Ernesto Elias-Nieland
Ranch Hand
Posts: 53
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic