Recursive Constructor Invocation in JavaIn Java, constructors are special methods that are called when an object is created. They are used to initialize the object state and carry out any important setup. One interesting feature of constructors in Java is the ability to name some other constructor inside the same class, that is called recursive constructor invocation. Recursive constructor invocation is a useful function of Java and different object-orientated programming languages, however it needs to be used cautiously to keep away from errors and infinite loops. It may be especially beneficial for simplifying the creation of objects with a subset of parameters, however it is far essential to continually make sure that the recursion subsequently reaches a base case and does now not retain indefinitely. Approach:ALGORITHM:Step 1: Start the program. Step 2: Define a class named Product. Step 3: Inside the Product class we have to declare the two instance variables name to store the name of the product and price to store the price of the product. Step 4: now create the constructor with two parameters name and price. Step 5: Inside the constructor, perform recursive constructor invocation by calling this(name, price). Step 6: Assign the name parameter to the call instance variable and the price parameter to the charge example variable. Step 7: Define a main method to test the Product class. Step 8: Inside the main method, create a Product object named product with the name "Phone" and price 999.99. Step 9: Print the product name and price using the System.out.println statement. Step 10: End the program. Implementation:The implementation of the above steps are given below FileName: Product.java Output: ERROR! javac /tmp/0qLw40OAxb/Product.java /tmp/0qLw40OAxb/Product.java:7: error: recursive constructor invocation public Product(String name,double price) ^ 1 error Explanation: The above code gives the recursive constructor invocation error now we are going to modify the code and given below ALGORITHM:Step 1: Start the program. Step 2: Define a class named Product. Step 3: Inside the Product class we have to declare the two instance variables name to store the name of the product and price to store the price of the product. Step 4: now create the constructor with two parameters name and price. Step 5: Inside the constructor, perform recursive constructor invocation by calling this(name, price). Step 6: Assign the name parameter to the call instance variable and the price parameter to the charge example variable. Step 7: Within the same constructor now create a new Product object named product1 with the name "Laptop" and price 2016.54. Step 8: Define a main method to test the Product class. Step 9: Inside the main method, create a Product object named product with the name "Phone" and price 999.99. Step 10: Print the product name and price using the System.out.println statement. Step 11: End the program. Implementation:The implementation of the above steps given below FileName: Product.java Output: Exception in thread "main" java.lang.StackOverflowErrorat Product. Basically, Stack overflow error occurs when we are not able to terminate all the conditions to the recursive function or the template, It will leads to the infinite loop and responsible for causing error. Next TopicJava Language / What is Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India