Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types.

Here is a simple example of constructor overloading in Java:

class Rectangle {
    // instance variables
    private int length;
    private int breadth;

    // Constructor with no parameter
    Rectangle() {
        length = 5;
        breadth = 2;
    }

    // Constructor with two parameters
    Rectangle(int l, int b) {
        length = l;
        breadth = b;
    }

    // Method to calculate area
    public int getArea() {
        return length * breadth;
    }

    public static void main(String args[]) {
        // Creating objects
        Rectangle rect1 = new Rectangle();
        Rectangle rect2 = new Rectangle(4, 6);

        // Displaying area of rectangles
        System.out.println("Area of rect1: " + rect1.getArea());
        System.out.println("Area of rect2: " + rect2.getArea());
    }
}

In the above program, we have two constructors: one without any parameters and one with two parameters. When we create an object rect1 using the no-argument constructor, the default values are used. When we create another object rect2 using the parameterized constructor, the values provided are used. This is a simple example of constructor overloading in Java.

This problem has been solved

Similar Questions

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

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

Why do we use constructor overloading?Select one:To use different types of constructorsTo differentiate one constructor from anotherBecause it’s a feature providedTo initialize the object in different ways

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.

Which among the following best describes constructor overloading?Select one:Defining destructor with each constructorDefining more than one constructor in single class with different signatureDefining more than one constructor in single classDefining one constructor in each class of a program

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.