Computer Applications
What is a no-argument constructor? Does every class have a no-argument constructor?
Java Constructors
3 Likes
Answer
A no-argument constructor is a constructor that accepts no arguments and has no statements in its body. It is also called the default constructor.
If the programmer has not explicitly defined a constructor for the class then Java compiler automatically adds a no-argument constructor/default constructor. However, if the constructor is defined explicitly then no-argument constructor is not added.
Answered By
1 Like
Related Questions
If a class is named DemoClass, what names are allowed as constructor names in the class DemoClass?
Explain the concept of constructor overloading with an example.
What is the use of the keyword this?
Create a class named Pizza that stores details about a pizza. It should contain the following:
Instance Variables:
String pizzaSize — to store the size of the pizza (small, medium, or large)
int cheese — the number of cheese toppings
int pepperoni — the number of pepperoni toppings
int mushroom — the number of mushroom toppingsMember Methods:
Constructor — to initialise all the instance variables
CalculateCost() — A public method that returns a double value, that is, the cost of the pizza.
Pizza cost is calculated as follows:- Small: Rs.500 + Rs.25 per topping
- Medium: Rs.650 + Rs.25 per topping
- Large: Rs.800 + Rs.25 per topping
PizzaDescription() — A public method that returns a String containing the pizza size, quantity of each topping, and the pizza cost as calculated by CalculateCost().