-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopy_Constructor.java
49 lines (42 loc) · 1013 Bytes
/
Copy_Constructor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class OOPS {
public static void main(String args[]) {
Student s1 = new Student();
s1.name = "Simran";
s1.roll = 123;
s1.password = "abcd";
s1.marks[0] = 100;
s1.marks[1] = 80;
s1.marks[2] = 80;
Student s2 = new Student(s1);
s2.password = "xyz";
s1.marks[2] = 100;
for(int i = 0; i<3; i++){
System.out.println(s2.marks[i]);
}
}
}
class Student{
String name;
int roll;
String password;
int marks[];
//copy contructor //shallow copy constrcutor
Student(Student s1){
marks = new int[3];
this.name = s1.name;
this.roll = s1.roll;
this.marks = s1.marks;
}
Student() {
marks = new int[3];
System.out.println("Constructor");
}
Student(String name){
marks = new int[3];
this.name = name;
}
Student(int roll) {
marks = new int[3];
this.roll = roll;
}
}