0% found this document useful (0 votes)
52 views

Lec-8b Copy Constructor

Uploaded by

MG Master Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Lec-8b Copy Constructor

Uploaded by

MG Master Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

OBJECT ORIENTED

PROGRAMMING
LECTURE # 8B: COPY
CONSTRUCTOR

Lecturer Muhammad Khalid


[email protected]

Department of CS
1 BSSE-2
Hitec University Taxila
COPY CONSTRUCTOR
When we pass object reference to the constructor
then it’s called copy constructor.
Copy constructor in java is a constructor which
copy the value of one object to another.
A copy constructor in programming, particularly in
languages like C++ and Java, is used to create a
new object as a copy of an existing object. It's
essentially a special constructor that allows you to
create a new object by copying the values of
another object of the same class.

2
BASIC ALGORITHM FOR IMPLEMENTING A COPY
CONSTRUCTOR IN JAVA:
Create a Class: Define a class for which you want to
implement the copy constructor. For example, let's
consider a class named MyClass.
Define Instance Variables: Declare the instance
variables that represent the state of objects of your class.
These variables define the attributes or properties of
objects.
Create a Constructor: Define a constructor to initialize
the instance variables of the class. This constructor will be
used to create objects of the class.
Implement the Copy Constructor: Define another
constructor that takes an object of the same class as a
parameter. This constructor will be used to create a copy
of an existing object. Inside this constructor, assign the
values of the instance variables of the parameter object to 3
the corresponding instance variables of the current object.
PROGRAM 1
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;
}
4
PROGRAM 1
//copy constructor
Student(Student student)
{
System.out.println("\n---Copy Constructor Invoked---");
roll = student.roll;
name = student.name;
}

5
PROGRAM 1
//method to return roll number
int printRoll()
{
return roll;
}
//Method to return name of the student
String printName()
{
return name;
}

6
PROGRAM 1
//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());
}
}
7
CODE 2
class MyClass {
// Step 2: Define instance variables
private int intValue;
private String stringValue;

// Step 3: Create a constructor


public MyClass(int intValue, String stringValue)
{
this.intValue = intValue;
this.stringValue = stringValue;
8
}
CODE 2
// Step 4: Implement the copy constructor
public MyClass(MyClass original) {
this.intValue = original.intValue;
this.stringValue = original.stringValue;
}
// Method to display details
public void displayDetails() {
System.out.println("Integer Value: " + intValue);
System.out.println("String Value: " + stringValue);
}
} 9
CODE 2
public class Main {
public static void main(String[] args) {
// Create an object using the constructor
MyClass originalObject = new MyClass(10, "Hello");
// Create a copy of the object using the copy constructor
MyClass copiedObject = new MyClass(originalObject);
// Display details of both objects
System.out.println("Original Object:");
originalObject.displayDetails();
System.out.println("\nCopied Object:");
copiedObject.displayDetails();
}
} 10
QUIZ # 3
Develop a Java program to accomplish each of the following tasks:
Create a Class named “Employee” and declare two protected
instance variables named “name” and “salary” of String and double
type respectively.
Write the constructor for the “Employee” class and initialize the
instance variables using this reference keyword.
Write a method named “displayInfo” that prints the values of
instance variables.
Next, create a subclass named “Manager” which extends the
“Employee” class.
Write a method named “displayInfo” in the “Manager” class to
override the same method in the “Employee” class and include the
department information.
Create another class “Main” that includes the main method.
Create an object of the “Manager” class in the main method and
pass the following values by the constructor: “John”, 50000, “HR”. 11
Call the “displayInfo” method from the main method.

You might also like