Table of Contents
Constructor overloading in Java language
In this tutorial, we will discuss the subject of Constructor overloading in Java language
Constructor overloading
Constructor overloading is one of the techniques in Java. A Java class can have any number of constructors that is different in the parameter. As the constructor has the same name of the class in Java, the compiler differentiates it by the number of parameters and their type. Same constructor existing multiple times in the same class with a different number, a different order and different type of parameter is called as constructor overloading. Constructor overloading looks like method overloading in Java.
public class myschool{
myschool(){ //constructor 1
System.out.println(“my primary school is puttalai”);
}
myschool(int i){ //constructor 2
System.out.println(“my secoundary school hartley”);
}
myschool(String str){ //constructor 3
System.out.println(“my Advanced level school hartley”);
}
public static void main(String args[]){
myschool pri=new myschool();
//object for first constructor
myschool sci=new myschool(10);
//object for second constructor
myschool adv=new myschool(“sam”);
//object for third constructor
}
}
In the example above, a class has three constructors with the same name myschool();.The first one is the default constructor and the others are parameterised constructors. Every parameterised constructor differs by the parameter list. So constructors can be overloaded.
Example 2
public class myschool{
myschool(){ //constructor 1
System.out.println(“my primary school is puttalai”);
}
myschool(int i){ //constructor 2
System.out.println(“my secoundary school hartley”);
}
myschool(String str){ //constructor 3
System.out.println(“my Advanced level school hartley”);
}
myschool(int x,int y){ //constructor 4
System.out.println(“my high level school JNCOE”);
}
myschool(String a,String b){ //constructor 5
System.out.println(“my degree level school CUSrilanka”);
}
public static void main(String args[]){
myschool pri=new myschool(); //object for first constructor
myschool sci=new myschool(10); //object for second constructor
myschool adv=new myschool(“sam”); //object for third constructor
myschool deg=new myschool(15,25); //object for forth constructor
myschool hei=new myschool(“kamal”,”suman”);
//object for fifth constructor
}
}
In the example above, a class has five constructors with the same name myschool();. The first one is the default constructor and the others are parameterised constructors. Every parameterised constructor differs by a parameter list. So a constructor can be overloaded and display an output.
Same name and parameter structured constructor can not be overloaded
public class myschool{
myschool(){ //constructor 1
System.out.println(“my primary school is puttalai”);
}
myschool(int i){ //constructor 2
System.out.println(“my secoundary school hartley”);
}
myschool(String str){ //constructor 3
System.out.println(“my Advanced level school hartley”);
}
myschool(String str){ //constructor 4
System.out.println(“my Advanced level school center college”);
}
public static void main(String args[]){
myschool pri=new myschool();
//object for first constructor
myschool sci=new myschool(10);
//object for second constructor
myschool adv=new myschool(“sam”);
//object for third constructor
myschool adv=new myschool(“dam”);
//object for third constructor
}
}
In the program above, constructors 3 and 4 are same structured constructor therefore they can not be overloaded.
Related Links
Method in Java Constructor in Java Method overloading in java
Abstraction in C++ Abstraction in Java
Encapsulation in C++ Encapsulation in Java
Polymorphism in C++ Methodoverriding in Java
Method overloading in java Constructor overloading in Java
Exception Handling in Java Method over riding in Python