Explain constructor with argument and without argument
import java.util.ArrayList;
public class Products {
private int price;
private String productsId;
private ArrayList<Products> products;
/*
* No-argument constructor: A constructor that has no parameter is known as the
* default constructor. If we don’t define a constructor in a class, then the
* compiler creates default constructor(with no arguments) for the class. And if
* we write a constructor with arguments or no-arguments then the compiler does
* not create a default constructor. In our example,one-word saing, we have some
* product but actually don't know about him anything because don't know his
* characteristics.
*/
public Products() {
}
/*
* A constructor that has parameters is known as parameterized constructor. If
* we want to initialize fields(characteristics,values) of the class with our own values, then use a
* parameterized constructor.
*/
public Products(int price, String productsId) {
this.price = price;
this.productsId = productsId;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getProductsId() {
return productsId;
}
public void setProductsId(String productsId) {
this.productsId = productsId;
}
public ArrayList<Products> getProducts() {
return products;
}
public void setProducts(ArrayList<Products> products) {
this.products = products;
}
@Override
public String toString() {
return "Products [price=" + price + ", productsId=" + productsId + ", products=" + products + "]";
}
Comments
Leave a comment