Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8278401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:59:42+00:00 2026-06-08T08:59:42+00:00

I read Effective Java book and don’t understand one paragraph where explained Clonable interface.

  • 0

I read Effective Java book and don’t understand one paragraph where explained Clonable interface. Can someone explain me this paragraph:

…programmers assume that if they extend a class and invoke
super.clone from the subclass, the returned object will be an instance
of the subclass. The only way a superclass can provide this
functionality is to return an object obtained by calling super.clone.
If a clone method returns an object created by a constructor, it will
have the wrong class.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:59:43+00:00Added an answer on June 8, 2026 at 8:59 am

    I should note to begin with that clone in and of itself is broken, and that a copy constructor, like Sheep(Sheep cloneMe) is a far more elegant idiom than clone, considering the Cloneable contract is very weak. You probably already know this, since you’re reading the book, but it’s worth putting in here.

    Anyway, to answer the question:

    Object.clone() will create an object of the same type as the object it was called on. For this reason, it is strongly encouraged to “cascade” up to Object for getting the result you plan to return. If someone decides to not follow this convention, you will end up with an object of the type of the class that broke the convention, which will cause a multitude of problems.

    To illustrate I have a class like so

    class Sheep implements Cloneable {
    
        Sheep(String name)...
    
        public Object clone() {
            return new Sheep(this.name); // bad, doesn't cascade up to Object
        }
    }
    
    class WoolySheep extends Sheep {
    
        public Object clone() {
            return super.clone();
        }
    }
    

    Suddenly, if I do

    WoolySheep dolly = new WoolySheep("Dolly");
    WoolySheep clone = (WoolySheep)(dolly.clone()); // error
    

    I’ll get an exception because what I get back from dolly.clone() is a Sheep, not a WoolySheep.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read Effective Java by Joshua Bloch and removed the 'Constant Interface anti-pattern' from
I'm trying out Effective Java book and enjoying it. I read Builder pattern and
It was suggested that Effective Java is a great book to read before programming
I'm using some functionality in Java that I don't really understand so I want
In Josh Bloch's excellent book Effective Java under Item 39 he says: [D]efensive copies
I read from Effective Java that In the absence of synchronization the following sequence
I read Effective Java, and there written If a class cannot be made immutable,
I read Effective Java and there it's stated that a singleton is best implemented
Read (skimmed enough to get coding) through Erlang Programming and Programming Erlang . One
I want to read text file's comma separated variables in a java script array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.