in Education by
Can we overload the constructors?

1 Answer

0 votes
by

Can we overload the constructors?

Yes, the constructors can be overloaded by changing the number of arguments accepted by the constructor or by changing the data type of the parameters. Consider the following example.

class Test   

{  

    int i;   

    public Test(int k)  

    {  

        i=k;  

    }  

    public Test(int k, int m)  

    {  

        System.out.println("Hi I am assigning the value max(k, m) to i");  

        if(k>m)  

        {  

            i=k;   

        }  

        else   

        {  

            i=m;  

        }  

    }  

}  

public class Main   

{  

    public static void main (String args[])   

    {  

        Test test1 = new Test(10);  

        Test test2 = new Test(12, 15);  

        System.out.println(test1.i);  

        System.out.println(test2.i);  

    }  

}  

      

In the above program, The constructor Test is overloaded with another constructor. In the first call to the constructor, The constructor with one argument is called, and i will be initialized with the value 10. However, In the second call to the constructor, The constructor with the 2 arguments is called, and i will be initialized with the value 15.

Related questions

0 votes
    What are the differences between the constructors and methods?...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
    Can you make a constructor final?...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
    What do you understand by copy constructor in Java?...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
    Is constructor inherited?...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
    Does constructor return any value?...
asked Nov 17, 2020 in Education by Editorial Staff
0 votes
    What is the constructor?...
asked Nov 17, 2020 in Education by Editorial Staff
0 votes
    Our core application changed from Python 2.6 to Python 2.7 maybe to Python 3 in later release. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I'm trying to mock a TypeScript class with Jest and I'm obviously doing something because receive the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I asked a question like this here: How to adjust Label in tkinter? But the events load up and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    ___________ is a type of DoS threats to overload a server as it sends a large number of requests requiring ... -for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    How to print details in the pattern given in java constructors like this Write a program that would print the ... - WallsStreat ?? Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    How to print details in the pattern given in java constructors like this Write a program that would print the ... - WallsStreat ?? Select the correct answer from above options...
asked Dec 18, 2021 in Education by JackTerrance
0 votes
    What is the return type of Constructors? (a) int (b) float (c) void (d) none of the mentioned ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    Which of these constructors is used to create an empty String object? (a) String() (b) String(void ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which of these exceptions is thrown by URL class's constructors? (a) URLNotFound (b) URLSourceNotFound (c) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
...