Oop Java

Constructor overloading in Java programming

Constructor overloading in Java programming

We will discuss Constructor overloading in Java programming

A Java class can have multiple constructors with the same name but it must have the differing list of parameters it is called as constructor overloading

Java allows constructor overloading like method overloadingThe constructor is just a method but is not a return type.

So same constructor can exist multiple time in the same class in Java with the different number of or different order of parameter or the different type of parameter.

Explanation of constructor overloading

Constructor overloading

 

class Cons_Over{
Cons_Over()//constructor 1 No parameter
{
 System.out.println("This is no argument constructor");
}
Cons_Over(int a)//constructor 2 - single parameter 
{
 System.out.println("This is parameterized constructor with one parameter");
}
Cons_Over(int a,String name)//constructor 3 - multiple parameter 
{
 System.out.println("This is parameterized constructor with two parameter");
}
Cons_Over(String name,int a)//constructor 4 - //multiple parameter different order
{
 System.out.println("This is parameterized constructor with two parameter different order");
 System.out.println("Constructors are overloaded");
}

public static void main(String args[]){
Cons_Over C=new Cons_Over();
Cons_Over C1=new Cons_Over(45);
Cons_Over C2=new Cons_Over(34,"khan");
Cons_Over C3=new Cons_Over("Jhon",56);
}

}

When the above code is executed, it produces the following results:

This is no argument constructor
This is parameterized constructor with one parameter
This is parameterized constructor with two parameter
This is parameterized constructor with two parameter different order
Constructors are overloaded

Program 2

class Student_Db{
int Age;
String Name;
float Avg;
Student_Db(){//constructor 1 no parameter
}

Student_Db(int Age){//constructor 2 single parameter
 this.Age=Age;
}
Student_Db(int Age,String Name){//constructor 3 multiple parameter
 this.Age=Age;
 this.Name=Name;
}
Student_Db(int Age,String Name,float Avg){//constructor 4 multiple parameter
 this.Age=Age;
 this.Name=Name;
 this.Avg=Avg;
}
void display(){
    System.out.println("This student absent for school");
}
void display1(){
     System.out.println("This student age is :"+Age);
}
void display2(){
    
     System.out.println("This student age is :"+Age+" Name is "+Name);
}
void display3(){
     System.out.println("This student age is :"+Age+" Name is "+Name+" id is :"+Avg);
}

public static void main(String args[]){
Student_Db s=new Student_Db();
Student_Db s1=new Student_Db(34);
Student_Db s2=new Student_Db(33,"Jhon");
Student_Db s3=new Student_Db(26,"Yaan",34.56f);
s.display();
s1.display1();
s2.display2();
s3.display3();

}

}

When the above code is executed, it produces the following results:

This student absent for school
This student age is :34
This student age is : 33  Name is Jhon
This student age is : 26 Name is Yann  id is : 34.56

 

Constructor with  getter and setter methods

This is an example for the constructor with getter and setter methods

class EmpDetails
{
private int empId;
private String empName;
private double empSalary;//Instance variable

EmpDetails(){ //default constructor
empId=100;
empName="Harunika";
empSalary=34567.89;

}

EmpDetails(int empId1,String empName1,double empSalary1){
//parameterized constructor - 1
empId=empId1;
empName=empName1;
empSalary=empSalary1;

}

EmpDetails(int empId1,String empName1){
//parameterized constructor - 2
this.empId=empId1;
this.empName=empName1;

}

//getter method
public int getEmpId(){
    return empId;
}

setter method
public void setEmpId(int empId){
    this.empId=empId;
}

public String getEmpName(){
    return empName;
}

public void setEmpName(String empName){
    this.empName=empName;
}

public double getEmpSalary(){
    return empSalary;
}

public void setEmpSalary(double empId){
    this.empSalary=empSalary;
}

public static void main(String args[]){
EmpDetails obj=new EmpDetails(); 
//object is created for Default constructor
System.out.println("Employee id is :"+obj.getEmpId());
System.out.println("Employee name is :"+obj.getEmpName());
System.out.println("Employee salary is :"+obj.getEmpSalary());

System.out.println(".....................");

EmpDetails obj1=new EmpDetails(345,"Kanth",23456.78); 
//object is created for parameterized constructor -1
System.out.println("Employee id is :"+obj1.getEmpId());
System.out.println("Employee name is :"+obj1.getEmpName());
System.out.println("Employee salary is :"+obj1.getEmpSalary());
System.out.println(".....................");

EmpDetails obj2=new EmpDetails(234,"Saru");
//object is created for parameterized constructor -2
System.out.println("Employee id is :"+obj2.getEmpId());
System.out.println("Employee name is :"+obj2.getEmpName());;

}
}

 

 

When the above code is executed, it produces the following results:

Employee id is: 100

Employee name is: Harunika

Employee salary is: 34567.89

………………………………………

Employee id is: 345

Employee name is: Kanth

Employee salary is: 23456.78

……………………………………….

Employee id is: 234

Employee name is: Saru

 

There are other Java language post that is similar to this post

The constructor in Java language

 

Suggested for you

Method in Java language

Method overloading in Java language

Method overriding in Java language

Constructor in C++ language

Constructor overloading in C++ language

 

Method in Java programming language with example
Constructor in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago