Knowee
Questions
Features
Study Tools

Illustrate the concept of constructor overloading through a java program which when given an input 1 will output ‘This is the parrot’ and when given an input 2 will output ‘this is the main’ and when given an input 3 will output ‘This is the 20 years sparrow’.Input:Input 1Output :This is the parrotInput:Input 2Output :this is the mainInput:Input 3Output :This is the 20 years sparrow

Question

Illustrate the concept of constructor overloading through a java program which when given an input 1 will output ‘This is the parrot’ and when given an input 2 will output ‘this is the main’ and when given an input 3 will output ‘This is the 20 years sparrow’.Input:Input 1Output :This is the parrotInput:Input 2Output :this is the mainInput:Input 3Output :This is the 20 years sparrow

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple Java program that uses constructor overloading to achieve the desired output:

public class Bird {
    // Default constructor
    public Bird(int i) {
        if(i == 1) {
            System.out.println("This is the parrot");
        } else if(i == 2) {
            System.out.println("This is the main");
        } else if(i == 3) {
            System.out.println("This is the 20 years sparrow");
        }
    }

    public static void main(String[] args) {
        Bird bird1 = new Bird(1);
        Bird bird2 = new Bird(2);
        Bird bird3 = new Bird(3);
    }
}

In this program, we have a class Bird with a constructor that takes an integer as an argument. Depending on the value of the integer, it prints different messages. In the main method, we create three instances of Bird with different input values (1, 2, 3), which results in different output messages.

This problem has been solved

Similar Questions

what is constructor overloading in java? Explain it with suitable program

Which of the following complete the constructor so that this code prints out 50? (Choose all that apply)public class Cheetah {int numSpots;public Cheetah(int numSpots) {// INSERT CODE HERE}public static void main(String[] args) {System.out.println(new Cheetah(50).numSpots);}}*A. numSpots = numSpots;B. numSpots = this.numSpots;C. this.numSpots = numSpots;D. numSpots = super.numSpots;E. super.numSpots = numSpots;F. None of the above.

What is method overloading in java?Question 14Answera.Defining a method in a subclass with the same signature as in its super class.b.Inheriting methods from a super class.c.Creating multiple constructors in the same class.d.Defining multiple methods with the same name but different parameter lists

A constructor with arguments is known as.

Which of the following statements is NOT true about constructors?They can be overloaded.They have the same name as the class.They can be used to allocate memory for the object.They can initialize member variables.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.