Attack of the Clones - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Attack of the Clones

Description:

It's called the clone method, and it is defined in Object. ... When you call the super.clone() method for a direct descendent of Object, it ... – PowerPoint PPT presentation

Number of Views:112
Avg rating:3.0/5.0
Slides: 13
Provided by: alan115
Category:
Tags: attack | clone | clones

less

Transcript and Presenter's Notes

Title: Attack of the Clones


1
Attack of the Clones
  • Dont Tell George I Used His Title.

2
Copying Objects
  • For primitive types, other than arrays, copying a
    value is simple.
  • int x 13
  • int y x
  • y 12
  • After the initialization of y, y is 13. After
    the reassignment, it is 12. What is x?

3
But, Objects Are Passed by Reference.
  • Look at the following
  • List x new List
  • List y x
  • y.clear()
  • After the initialization of y, y is the same
    list as x. After the clear, y is the empty list.
    What is x?

4
x and y
  • Objects are passed by reference.
  • x and y are simply references to the same object
  • Therefore, what happens to one, happens to the
    other.

x
y
5
Thats not always a good thing
  • What happens if we want to get a copy of a list
    to do other operations on, like reversing, or
    sorting, but we dont want to destroy the
    original list? We obviously cant copy through
    assignment. The answer is to clone the list.

6
The Cloneable Interface
  • Java provides a method for creating copies of
    objects. Its called the clone method, and it is
    defined in Object. However, they didnt want to
    make it too easy, because some objects shouldnt
    be copyable. Think about our list of 100000
    elements that we sorted. The ability to copy
    this could be very expensive, and so you should
    think twice before making it available.

7
But I need my copy!
  • If you decide that you want to make an object
    clonable, you need to do two things
  • First, have it implement the Cloneable interface.
  • Second, override the clone method, which is
    declared protected in Object.

8
  • public class foo implements Cloneable
  • public String data
  • public foo(String data)
  • this.data data
  • public Object clone()
  • try
  • return (foo)super.clone()
  • catch(CloneNotSupportedException e)
  • return null

9
  • Notice that you need to catch the
    CloneNotSupportedException that the clone method
    can throw. This is true, unless we are cloneing
    a subclass of another cloneable object that
    already catches the exception.
  • So, that was easy, right? We just whip up a
    simple call to clone, and everything is fine.
    Well, not exactly.

10
Shallow Copy vs. Deep Copy
  • When you call the super.clone() method for a
    direct descendent of Object, it performs a
    shallow copy of the object. What this means is
    that all primitive objects will be copied into
    our shiny new object, but all non-primitives will
    only have their references copied.

11
So, How To Make A Deep Copy?
  • As you might suspect, this takes more work. For
    each of the Object variables contained in your
    clone, you must clone the variables as well as
    the base object.

12
  • Node n new Node()
  • public Object clone()
  • List newList (List)super.clone()
  • newList.setNode((Node)n.clone())
  • return newList
Write a Comment
User Comments (0)
About PowerShow.com