Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. public class CopyConstructorDemo {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. Teacher teacher = new Teacher("Kripalu");
  6. Student sOrg = new Student(15007, "Amit", "Chirimiri", teacher);
  7.  
  8. //Student sClo = sOrg; //Java Reference
  9.  
  10. //Student sClo = (Student) sOrg.clone(); //CLONE
  11.  
  12.  
  13. Student sClo = new Student(sOrg); //COPY CONSTRUCTOR
  14.  
  15. sOrg.display();
  16.  
  17. sClo.getTeacher().setName("ShriKrishn");
  18.  
  19. sOrg.display();
  20.  
  21. }
  22.  
  23.  
  24. }
  25.  
  26. class Teacher implements Cloneable{
  27.  
  28. String _name = "";
  29.  
  30. Teacher(String name){
  31. this.setName(name);
  32. }
  33.  
  34. String getName(){return _name;}
  35. void setName(String name){_name = name;}
  36.  
  37. //For Deep copy
  38. //@Override
  39. protected Object clone(){
  40.  
  41. try {
  42. return super.clone();
  43. } catch (CloneNotSupportedException e) {
  44. e.printStackTrace();
  45. return null;
  46. }
  47.  
  48. }
  49. }
  50.  
  51. class Student implements Cloneable{
  52.  
  53. int _rollNo;
  54. String _name;
  55. String _address;
  56.  
  57. Teacher _teacher;
  58.  
  59. Student(int rollNo, String name, String address, Teacher teacher){
  60. this.setRollNo(rollNo);
  61. this.setName(name);
  62. this.setAddress(address);
  63.  
  64. _teacher = teacher;
  65. }
  66.  
  67. Student(Student copyCons){
  68. this._rollNo = copyCons._rollNo;
  69. this._name = copyCons._name;
  70. this._address = copyCons._address;
  71. this._teacher = copyCons._teacher;
  72.  
  73. }
  74.  
  75. Teacher getTeacher(){return _teacher;}
  76. void setTeacher(Teacher teacher){_teacher = teacher;}
  77.  
  78. int getRollNo(){return _rollNo;}
  79. String getName(){return _name;}
  80. String getAddress(){return _address;}
  81.  
  82. void setRollNo(int rollNo){_rollNo = rollNo;}
  83. void setName(String name){_name = name;}
  84. void setAddress(String address){_address = address;}
  85.  
  86. void display(){
  87. System.out.println(_rollNo+" "+
  88. _name+" "+
  89. _address+" "+
  90. _teacher.getName());
  91. }
  92.  
  93. @Override
  94. protected Object clone(){
  95.  
  96. try {
  97.  
  98. //return super.clone(); //For Shallow copy
  99.  
  100. //For Deep copy
  101. Student cloned = (Student)super.clone();
  102. cloned.setTeacher((Teacher)cloned.getTeacher().clone());
  103. return cloned;
  104.  
  105. } catch (CloneNotSupportedException e) {
  106. e.printStackTrace();
  107. return null;
  108. }
  109.  
  110. }
  111.  
  112.  
  113.  
  114. }
  115.  
  116. Student(Student copyCons){
  117.  
  118. this._rollNo = copyCons._rollNo;
  119. this._name = copyCons._name;
  120. this._address = copyCons._address;
  121. this._teacher = (Teacher) copyCons._teacher.clone(); //FIX: thanks to Amir
  122.  
  123. }
  124.  
  125. //Copy constructor for the student
  126. Student(Student copyCons){
  127. this._rollNo = copyCons._rollNo;
  128. this._name = copyCons._name;
  129. this._address = copyCons._address;
  130. this._teacher = copyCons._teacher.clone();
  131. }
  132.  
  133. //Clone for the student
  134. protected Student clone(){
  135. return new Student(this);
  136. }
  137.  
  138. //This is the copy constructor
  139. Teacher(Teacher t){
  140. setName(t.getName());
  141. }
  142.  
  143. //That's how you clone an object of type teacher
  144. protected Teacher clone(){
  145. return new Teacher(this);
  146. }
  147.  
  148. Teacher t1 = new teacher("Teacher 1");
  149. Teacher t1Clone = t1.clone();
  150.  
  151. Student s1 = new Student(15007, "Amit", "Chirimiri", t1);
  152. Student s1Clone = s1.clone();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement