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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:39:57+00:00 2026-06-03T12:39:57+00:00

I have an abstract super class and a few derived class. I want to

  • 0

I have an abstract super class and a few derived class.
I want to create a method

createNew()   

when invoked on a derived class it will create a new instance of the derived with a smart copy of all of its members.
All the members that are important for the creation are in the abstract class so the code should be the same.
can I write the implementation of createNew() in the super class?

something like that

SonA sonA2 = sonA1.createNew()   
SonB sonB2 = sonB1.createNew()  

the abs superclass should do the implementation because the code is identical .
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-03T12:39:59+00:00Added an answer on June 3, 2026 at 12:39 pm

    The implementation should be split between the abstract and the concrete classes. You can do it using the Template Method pattern:

    public abstract class AbstractSon {
        protected abstract AbstractSon createNewImpl();
        public AbstractSon createNew() {
            AbstractSon res = createNewImpl();
            res.name = "default name";
            return res;
        }
    }
    public class Son1 extends AbstractSon {
        protected AbstractSon createNewImpl() {
            return new Son1();
        }
    }
    public class Son2 extends AbstractSon {
        protected AbstractSon createNewImpl() {
            return new Son2();
        }
    }
    

    You can split the responsibility differently to get the precise return type:

    public abstract class AbstractSon {
        protected void prepare(AbstractSon toPrepare) {
            toPrepare.name = "default name";
        }
    }
    public class Son1 extends AbstractSon {
        public Son1 createNew() {
            Son1 res = new Son1();
            prepare(res);
            return res;
        }
    }
    public class Son2 extends AbstractSon {
        public Son2 createNew() {
            Son2 res = new Son2();
            prepare(res);
            return res;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract base class and derived class: type TInterfaceMethod = class public
I have an abstract Java class MyAbstractClass with a private method. There is a
I have a abstract super class called RealAlgebraicNumber and two inherited classes called IntervalRepresentation
I have a super abstract class: @XmlSeeAlso({AndQuery.class, OrQuery.class, NotQuery.class, PropertyQuery.class, MultiQuery.class}) @XmlRootElement public abstract
I have an abstract class called camera which PointCamera uses as its super class.
I have an abstract method call in my super's constructor. I run into the
I have this super class which extends from another class public abstract class AbstractDOEMessageFinderAction
I have an abstract class Entity . Every class extending Entity will need some
I have abstract BaseController, which basically looks like below: public abstract class BaseController :
Let say I have abstract class called: Tenant and Customer. The tenant in 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.