• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Shallow and Deep Copy in Java

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 1:

Do we need to override the clone method if we implement the Cloneable interface, if there is no specific functionality defined.

If we have to override , what is the benefit of such override.

Question 2:

The below code is from other site (Source:http://javaconceptoftheday.com/difference-between-shallow-copy-vs-deep-copy-in-java/) ,

In the deep copy example


In the deep copy example the clone method definition is modified , even if we don’t override it and have another course object, add it to the student object and use the set method for changing the field , its going to behave the same.

What is the benefit of such override?

Example similar to the above:

Course course=new Course(“subj1″,”subj2”,"subj3");
Student std=new Student(123,“student1”,course);
Student std2=(Student ) std.clone();
Course course2=new Course(“subj1″,”subj2”,"subj3");
std2.setCourse(course2);

is this definition and the overrided clone method definition are the same?
 
Friends help you move. Good friends help you move bodies. This tiny ad will help:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic