Explain the difference between a parameterized constructor a
Explain the difference between a parameterized constructor and a default constructor. Provide an example class that includes both types of constructors.
Solution
Constructor is nothing but a method that has same name of class name. which does not have any return type and not even void. Constructor will invoke at the time of object instantiation.
Default constructor is a construtor which does not have any parameters. in simple words, constructor with no parameters is called default constructor.
Parameterized constructor is a construtor which has parameters in it. in simple words, constructor with parameters is called parameterized constructor.
Below is the example for the same
Test.java
public class Test {
private int a;//int variable declaration
private String s;//string variable declaration
public Test(){//default constructor
}
public Test(int a, String s){//parameterized constructor
this.a = a;//int variable assignment
this.s = s;//string variable assignment
}
}
