The clone() method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momotaro
    Contributor
    • Sep 2006
    • 357

    The clone() method

    why can't we just extend the class that will be using the clone method in order to be able to use it without overiding?
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    What happens when you try that?

    Comment

    • momotaro
      Contributor
      • Sep 2006
      • 357

      #3
      the same message clone is protected

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by API Specs
        The precise meaning of "copy" may depend on the class of the object.
        Realize also that to be able to produce clones of yourself, you must be Cloneable.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by momotaro
          the same message clone is protected
          Yes, because it was defined that way in the Object class; to make it public
          you have to override it (and call the superclass's implementation) and
          occasionally you have to implement the Cloneable marker interface as well
          (if that hasn't been done so already in a superclass).

          kind regards,

          Jos

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            See why clone must be overridden, do now you, hmm? Yeesssssss.

            Comment

            • momotaro
              Contributor
              • Sep 2006
              • 357

              #7
              but when we have a class inhereting from another if the superclass members are protected they are visible for the subclass! why its not the same thing for method clone!?

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by momotaro
                but when we have a class inhereting from another if the superclass members are protected they are visible for the subclass! why its not the same thing for method clone!?
                It is the same; that is what protected access rights are all about; *plus *the
                clone() method checks whether or not a particular class (or a parent thereof)
                implements the Cloneable interface.

                Suppose my father knows a famous protected beer brewing recipe. I can ask
                him to brew beer but you can't ask me to brew beer although my father knows
                how to do it.

                kind regards,

                Jos

                Comment

                • momotaro
                  Contributor
                  • Sep 2006
                  • 357

                  #9
                  Originally posted by Momotaro
                  So why am having that error in line 2 :
                  illigal start of expretion not astatement ';' expected
                  [CODE=java] public static void main(String[] args) {
                  throws CloneNotSupport edException {
                  CloneThatObject originalObject =
                  new CloneThatObject ("toto", "foo");
                  CloneThatObject copyObject = (CloneThatObjec t)
                  originalObject. clone();
                  }
                  }[/CODE]

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    You have too many curly brackets. Check your Java syntax; a throws clause
                    does not belong in the body of a method.

                    kind regards,

                    Jos

                    Comment

                    • BigDaddyLH
                      Recognized Expert Top Contributor
                      • Dec 2007
                      • 1216

                      #11
                      A demo an a few comments.

                      [CODE=Java]public class Example implements Cloneable {
                      private String text;

                      public void setText(String text) {
                      this.text = text;
                      }

                      public String getText() {
                      return text;
                      }

                      public Example(String text) {
                      setText(text);
                      }

                      @Override()
                      public String toString() {
                      return getText();
                      }

                      @Override()
                      public Example clone() {
                      try {
                      return (Example) super.clone();
                      } catch (CloneNotSuppor tedException e) {
                      throw new Error(e);
                      }
                      }

                      public static void main(String[] args) {
                      Example a = new Example("hello world");
                      Example b = a.clone();
                      System.out.prin tln(b);
                      }
                      }[/CODE]
                      1. I claim that most of the time (check your design) if a class has a public clone, it's intended that it and all its subclass will not throw CloneNotSupport edException, so I struck that from the method's signature.
                      2. Don't forget about covariant return types: http://www.java-tips.org/java-se-tip...urn-types.html

                      Comment

                      • momotaro
                        Contributor
                        • Sep 2006
                        • 357

                        #12
                        and how to hide the clone method from a class that extends a class implementing cloneable?

                        Comment

                        • BigDaddyLH
                          Recognized Expert Top Contributor
                          • Dec 2007
                          • 1216

                          #13
                          Originally posted by momotaro
                          and how to hide the clone method from a class that extends a class implementing cloneable?
                          I don't understand. Please explain.

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by momotaro
                            and how to hide the clone method from a class that extends a class implementing cloneable?
                            You can implement it and throw the CloneNotSupport edException (if I got your question right that is).

                            Comment

                            • momotaro
                              Contributor
                              • Sep 2006
                              • 357

                              #15
                              Originally posted by BigDaddyLH
                              I don't understand. Please explain.
                              I mean that I need the class to extend a cloneable one foe other reason but not the clone method to be visible

                              Comment

                              Working...