0 votes
in JAVA by

Constructor Overloading in Java with examples

1 Answer

0 votes
by

What is Constructor Overloading?

When a class has multiple constructors with different function definitions or different parameters it is called constructor overloading.

import java.io.*; 

import java.lang.*; 

public class constructor_overloading { 

    double sum; 

    constructor_overloading(){ 

        sum=0; 

    } 

    constructor_overloading(int x,int y){ 

        sum=x+y; 

    } 

    constructor_overloading(double x,double y){ 

        sum=x+y; 

    } 

    void print_sum(){ 

        System.out.println(sum); 

    } 

    public static void main(String args[]){ 

        constructor_overloading c=new constructor_overloading(); 

        c.print_sum(); 

        constructor_overloading c1=new constructor_overloading(10,20); 

        c1.print_sum(); 

        constructor_overloading c2=new constructor_overloading(10.11,20.11); 

        c2.print_sum(); 

    } 

🔗Reference: stackoverflow.com

🔗Source: Java Interview Questions and Answers

Related questions

0 votes
asked Oct 27, 2020 in JAVA by sharadyadav1986
+2 votes
asked May 31, 2020 in JAVA by SakshiSharma
...