Thursday , February 27 2020
Home / Java / JAVA #21 – Constructor Overloading in Java

JAVA #21 – Constructor Overloading in Java


Constructor-Overloading in Java

In some programming languages, constructor overloading is the ability to create multiple methods of the same name with different implementations in same class.

Syntax

public class DataArtist {
   ...
   DataArtist (String s) {
       ...
   }
   DataArtist (int i) {
       ...
   }
   DataArtist (double f) {
       ...
   }
   DataArtist (int i, double f) {
       ...
   }
}
// File name ContOverloading.java 
package contoverloading;
public class ContOverloading {
    public static void main(String[] args) {
        Addition objAdd = new Addition();
        Addition objAdd1 = new Addition(10);
        Addition objAdd2 = new Addition(10,35);
        Addition objAdd3 = new Addition(10,345,56);
    }  
}
// File name Addition.java must store in same package of above file.
package contoverloading;
public class Addition {
    Addition()// Default Consturtor
    {
        System.out.println("No Parameter Here");
    }
    Addition(int i)
    {
        System.out.println("The Value is :"+i);
    }
    Addition(int i, int j)
    {
        System.out.println("The Sum of Two variables is :"+(i+j));
    }
    Addition(int i, int j, int k)
    {
        System.out.println("The Sum of Three variables is :"+(i+j+k));
    }
}

 

 

About Faheem

Faheem
An adaptable and responsible Software Engineer, who enjoys working with computers and the opportunity to learn new skills and work with new technologies is particularly attractive to me.

Check Also

JAVA #32 – Java application using SQL server (Part: 01)

  In this part we made the GUI front end of a Application.

One comment

Leave a Reply

Your email address will not be published.