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)); } }
Great Work