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 272395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:18:33+00:00 2026-05-12T00:18:33+00:00

I have written this clone method for when the parent of the Employee class

  • 0

I have written this clone method for when the parent of the Employee class is abstract and the clone() method in the parent class is abstract.I wanted to copy the primitive data type of the Employee’s object with this code instead of copying each primitive data type individually, but this code has problem with the line that I call clone() method. (This code is in Employee class)

public Object clone() {
    Object obj = new Object();
    Object object = obj.clone();  //Emphasis here
    return object;

}

the error is: The method clone() from the type Object is not visible.

But my Employee class is in the class hierarchy which can access the protected clone() method in the Object class.

This is my simple Employee class:

public class Employee extends Person implements Cloneable {
private int ID;

public Employee() {
    ID = 0;
}

public void setID(int ID) {
    this.ID = ID;
}

public int getID() {
    return ID;
}

public Object clone1() throws CloneNotSupportedException {
    try {
        Object obj = new Object();

        Object object = obj.clone();
        return object;
    } catch (CloneNotSupportedException ex) {
        return null;
    }
}
  • 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-05-12T00:18:33+00:00Added an answer on May 12, 2026 at 12:18 am

    Did you implement the Cloneable interface on your object?

    However, there are very few cases I would use clone for copying an object. One such safe example is array.clone(). I would rather use the copy-constructor idiom or manually copying / assigning values around.

    There is Item#11 in Effective Java (2nd edition) about the background issue. Cloneable interface is a special kind of interface as it modifies the Object class’ behavior regarding the cloning. Basically it is a feature enabling classinterface in Java.

    Edit: Based on your example you might need to wrap the clone() call in a try-catch of CloneNotSupportedException in a general case.

    Edit2: Rephrased my answer

    Edit3: Did you override the clone() in the public context? In the sample you gave you try to clone an Object, which is in the java.lang package – hardly the package your code is in.

    Edit4: I think the answer is already in the other posts, just wanted to reflect on the underlying issue.

    Edit5: Try this:

    public Object clone1() throws CloneNotSupportedException {        
        return super.clone();        
    }
    

    Edit6 Then name your method public abstract Object copy() for example and in the implementation, use the super.clone() – to avoid confusion.

    Edit7 I did some eclipsing and came out with the following solution:

    public class Cloner {
        public static abstract class Person {
           protected abstract Object clone1() throws CloneNotSupportedException;
           public Object copy() throws CloneNotSupportedException {
               return clone1();
           }
        }
        public static class Employee extends Person implements Cloneable {
            @Override
            protected Object clone1() throws CloneNotSupportedException {
                return super.clone();
            }
        
        }
        public static void main(String[] args) throws Exception {
            new Employee().copy();
        }
    }
    

    But basically it is the same concept as renaming your abstract method to something else than clone().

    Edit8: Fixed my sample, now it works without exception.

    (But the actual credit goes to Gábor Hargitai for super.clone())

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

Sidebar

Related Questions

I have written this query for retrieving data from mysql as below select FeedbackCode,EMailID,FeedbackDetail,
I have written a method which writes an object to file. I used generics
I have a method that tries to clone an instance of a class (B)
I have a class that reads the written output of a Student[] array object
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have written this code to join ArrayList elements: Can it be optimized more?
I have written this code to convert string in such format 0(532) 222 22
I have written this code in javascript however I have to make it work
In my code I have written this but it fails to compile: In Class1.h:
I am learning Python for the past few days and I have written this

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.