Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Constructor overloading in Java language

Posted on January 30, 2017February 24, 2020

Table of Contents

  • Constructor overloading in Java language
    • Constructor overloading

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.

 

Constructor overloading in Java language
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.

Constructor overloading in Java language
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

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes