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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:31:28+00:00 2026-05-29T05:31:28+00:00

I have a hierarchy of interfaces, with Child implementing Parent . I would like

  • 0

I have a hierarchy of interfaces, with Child implementing Parent. I would like to work with immutable objects, so I would like to design Builder classes that construct these objects conveniently. However, I have many Child interfaces, and I don’t want to repeat the code for building Parents in each type of child builder.

So, assume the following definitions:

public interface Parent {
    public Long getParentProperty();
}

public interface Child1 extends Parent {
    public Integer getChild1Property(); 
}

public interface Child2 extends Parent {
    public String getChild2PropertyA();
    public Object getChild2PropertyB();
}

How can I efficiently implement builders Child1Builder and Child2Builder? They should support operation like:

Child1 child1 = Child1Builder.newChild1().withChild1Property(5).withParentProperty(10L);

and

Child2 child2 = Child2Builder.newChild2().withChild2PropertyA("Hello").withParentProperty(10L).withChild2PropertyB(new Object());

I don’t want to implement a special case of withParentProperty for each child builder.

Edited to add second property to Child2 to clarify that this cannot be done with simple generics. I am not looking for a way to combine Child1 and Child2 – I am looking for a way to implement a Builder system that does not duplicate the work of building the parent class for every child class.

Thanks for any help!

  • 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-29T05:31:29+00:00Added an answer on May 29, 2026 at 5:31 am

    The solution I imagine is like the Curiously Recurring Template Pattern, or CRTP. You can define a base class to handle the parent-related initialization, but you still may find the two boilerplate getParent() and getThis() methods to be too much repetition in each derived child-related builder class.

    Take a look:

    abstract class ParentBase implements Parent
    {
      @Override
      public final Long getParentProperty()
      {
          return parentProperty_;
      }
    
    
      protected void setParentProperty(Long value)
      {
          parentProperty_ = value;
      }
    
    
      private Long parentProperty_;
    }
    
    
    abstract class ParentBuilder<T extends ParentBuilder<T>>
    {
      T withParentProperty(Long value)
      {
          getParent().setParentProperty(value);
          return getThis();
      }
    
        
      protected abstract ParentBase getParent();
    
    
      protected abstract T getThis();
    }
    
    
    final class ConcreteChild1 extends ParentBase implements Child1
    {
      @Override
      public Integer getChild1Property()
      {
          return childProperty_;
      }
    
    
      public void setChild1Property(Integer value)
      {
          childProperty_ = value;
      }
    
    
      private Integer childProperty_;
    }
    
    
    final class Child1Builder extends ParentBuilder<Child1Builder>
    {
      public Child1Builder()
      {
         pending_ = new ConcreteChild1();
      }
    
    
      public Child1Builder withChild1Property(Integer value)
      {
          pending_.setChild1Property(value);
          return this;
      }
    
    
      @Override
      protected ParentBase getParent()
      {
          return pending_;
      }
    
    
      @Override
      protected Child1Builder getThis()
      {
          return this;
      }
    
    
      private final ConcreteChild1 pending_;
    }
    

    As you can see, the ParentBuilder type expects to be cooperating with a derived type to allow it to return a properly-typed instance. Its own this reference won’t do, because the type of this within ParentBuilder is, of course, ParentBuilder, and not, say, Child1Builder as intended to maintain the "fluent" call chaining.

    I owe the "getThis() trick" to Angelika Langer’s tutorial entry.

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

Sidebar

Related Questions

I have an hierarchy of classes that looks like the following: class Critical {
I have a hierarchy of three interfaces, grandparent, parent and child. Parent and child
I have the following interfaces that are part of an existing project. I'd like
I have two interfaces, a generic and a non-generic that have an inheritence hierarchy:
I have a hierarchy of classes like this: MyBox | |->ImageBox |->GalleryBox |->MovieBox |->
I have a set of classes and interfaces which have a relatively simple hierarchy,
Suppose that you have the following hierarchy of statistics-related classes, structured in a manner
Lets say I have hierarchy like this (This is just a test program. Please
For example I have a hierarchy of movie clips. mc1 is a child of
Let's say we already have a hierarchy of classes, e.g. class Shape { virtual

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.