java

Constructor overloading in Java language

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

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.

Example

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

 

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.