Answer to Question #248249 in Java | JSP | JSF for Tege

Question #248249

Explain constructor with argument and without argument


1
Expert's answer
2021-10-07T16:40:23-0400


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 + "]";
	}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS