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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:10:12+00:00 2026-05-24T13:10:12+00:00

I try to use a simple example for better undersanding: I’ve got a class

  • 0

I try to use a simple example for better undersanding: I’ve got a class Tool and child classes which are extending class Tool: Hammer, Saw. Both have defined some fields like weight and both are overriding method getCost with own implementation.

    Tool first_tool = new Hammer();
    Tool second_tool = new Saw();

I need a method in Tool class, that will to do a copy of any tool, such way, that first_tool_copy is from the same subclass as first_tool. How can I make this possible? I need something like:

    /* Copy tool, change parameters of copy, the original won't change */
    /* first_tool_copy will be instance of Hammer class */
    first_tool_copy = first_tool.copy
    first_tool_copy.weight = 100

Conclusions: I would like to have some simple copy constructor common for all subclasses.

  • 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-24T13:10:13+00:00Added an answer on May 24, 2026 at 1:10 pm

    I would make Tool abstract, and add an abstract copy method to Tool. Then each subclass is forced to provide its own implementation. This is a fairly OO approach, and takes advantage of dynamic dispatch.

    abstract class Tool
    {
        // snip...
        public abstract Tool copy();
        // snip...
    }
    
    class Hammer
    {
        // snip...
        public Tool copy()
        {
            Hammer h = new Hammer();
            // copy fields from this to h
            return h;
        }
        // snip...
    }
    

    Otherwise, you’d provide a concrete implementation in Tool which you’d have to update every time to want to handle a new subclass of Tool. This method would have to use instanceof, getClass(), or similar non-OO techniques to create the right class. Ew.


    Remember what Effective Java Item 10 tells us: Override clone judiciously.


    I should mention, that copy is made just by copying the weight attribute.

    Assuming the Tool implementation looks something like the below class, you could do this with reflection:

    class Tool
    {
        // ...
    
        public Tool () {}
    
        public int getWeight ()
        {
            // don't care about implementation
        }
    
        public void setWeight()
        {
            // don't care about implementation
        }
    
        // ignores all exceptions - not production code!
        public Tool copy() throws Exception
        {
            Tool copy = this.getClass().getConstructor().newInstance();
            copy.setWeight(this.getWeight());
            return copy;
        }
    
        // ...
    }
    

    I wouldn’t recommend this, though. It’s just smelly and ugly to me.

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

Sidebar

Related Questions

I try to use doctest from example from http://docs.python.org/library/doctest.html But when I run python
I try to use PHPMailer to send registration, activation. etc mail to users: require(class.phpmailer.php);
I try to use this code for toggling text on the link: var showText=<span>Open</span>
I try to use the domainpeople.com API and to do I need to use
I try to use the winDBG to debug a dump file. When I run
I try to use the following code: ArrayList<String> Map<String, String> Eclipse complains about both
I try to use single quotes as much as possible and I've noticed that
I try to use amazon API using PHP. If I use print_r($parsed_xml->Items->Item->ItemAttributes) it show
I try to use the SQL statement SELECT * FROM table ORDER BY column
I try to use this mapping : @Entity @Table(name=ecc.\RATE\) @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name=DISCRIMINATOR, discriminatorType= DiscriminatorType.STRING) public

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.