1. Department of Computer Science, COMSATS University Islamabad - Wah Campus - Pakistan
Constructor overloading,
constructor chaining, and copy
constructor
(Java)
2. Contents
Recap of previous lecture
Function overloading
Constructor overloading
Constructor chaining
Copy constructor
References
Next lecture
2
3. Recap of previous lecture/week
3
• Default – No keyword required
• Public
• Private
• Protected
• Default constructor
• No-argument constructor
• Parametrized constructor
4. Function overloading
4
First understand or revise the concept of function overloading
• Method Overloading is a
feature that allows a class to
have more than one method
having the same name, if
their argument lists are
different.
• It is similar to constructor
overloading in Java, that
allows a class to have more
than one constructor having
different argument lists.
5. Constructor overloading
5
• Constructor can be overloaded
• An overloaded constructor provides multiple ways to set up a new object
• Complier differentiates which constructor is to be called depending on the
signature (type, sequence, number of arguments).
• When we construct an object, the complier decides which constructor to invoke
according to the type of the actual parameter.
8. Constructor chaining
8
• Constructor Chaining is the process of calling one constructor of a class from
another constructor of the same class or another class using the current object of
the class.
• There are two ways by which we can use constructor chaining in Java. These ways
depend on whether we are using it in the same class or the different class.
Using this keyword
Using the super keyword (will discuss later)
9. Constructor chaining example
9
package ThisExample;
class Example
{
//non-parametrized constructor
Example()
{
System.out.println("Inside non-parametrized ");
}
//Parameterized constructor
Example(double x)
{
//calling non-parametrized constructor
this();
System.out.println("Inside Parameterized“+x);
}
package ThisExample;
public class ThisExample
{
public static void main(String arg[])
{
//calling the parameterized constructor
Example obj = new Example(15);
}
}
//Parameterized constructor
Example(int n)
{
//calling parametrized constructor
this(4.5);
System.out.println("Inside Parameterized“+n);
}
}
10. Copy constructor
10
• A Copy Constructor in Java is a special type of constructor that is used to create a
new object using the existing object of a class that we have created previously.
• It creates a new object by initializing the object with the instance of the same
class.
• The Java Copy Constructor provides a copy of the specified object by taking the
argument as the existing object of the same class.
• Java does not implicitly provide the facility of a Copy constructor as in C language.
• But, we can define it by copying the values of one object to another object.
11. Creating a Copy Constructor in Java
11
• To create a copy constructor in java, we need to first declare a constructor that
takes an object of the same type as a parameter. For example:
public class Student
{
private int roll ;
private String name;
// Declaring a copy constructor by passing the parameter as the class
public Student( Student student )
{ }
}
12. Creating a Copy Constructor in Java
12
• After declaring a copy constructor, we need to copy each field of the input object
of the class into the new object. For example:
public class Student
{
private int roll;
private String name;
// Copying each field of the existing object into the newly created object
public Student( Student student )
{
this.id = student.roll;
this.name = student.name;
}
}
13. Copy Constructor example
13
package Xyz;
public class Student
{
private int roll;
private String name;
//constructor to initialize roll number and name of the student
Student(int rollNo, String sName)
{
roll = rollNo;
name = sName;
}
//copy constructor
Student(Student student)
{
System.out.println("n---Copy Constructor Invoked---");
roll = student.roll;
name = student.name;
}
//method to return roll number
int printRoll()
{
return roll;
}
//Method to return name of the student
String printName()
{
return name;
}
}
14. Copy Constructor example
14
//class to create student object and print roll number and name of the student
package Xyz;
public class Xyz
{
public static void main(String[] args)
{
Student student1 = new Student(101, “ali");
System.out.println("Roll number of the first student: "+ student1.printRoll());
System.out.println("Name of the first student: "+ student1.printName());
//passing the parameter to the copy constructor
Student student2 = new Student(student1);
System.out.println("nRoll number of the second student: "+ student2.printRoll());
System.out.println("Name of the second student: "+ student2.printName());
}
}
Roll number of the first student: 101
Name of the first student: Ali
—Copy Constructor Invoked—
Roll number of the second student: 101
Name of the second student: Ali