Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)
Is it possible to call one constructor from another? If yes then how can we do this?

1 Answer

0 votes
by (13.1k points)

You can do this by a property called constructor chaining. To chain a constructor to another in the same class we have to use this pointer, like this:

public class something{

int sum;

 something(){

this(8,5);

}

something(int x1,int x2){

this.sum=x1+x2;

}

}

In case you have to use the constructor in different class use super

public class something1{

int sum;

public something1(int x1,x2){

this.sum=x1+x2;

}

}

public class something2 extends something1{

something2(){

super(8,5);

}
}

Want to learn java? Check out java certification from Intellipaat.

Related questions

Browse Categories

...