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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:20:44+00:00 2026-05-25T17:20:44+00:00

Its hard to explain in word what I’m after but hopefully the code example

  • 0

Its hard to explain in word what I’m after but hopefully the code example below with the comments is sufficient. Basically I want the SubClass sc = new Subclass().method1() line to return the Subclass instance.

public class SuperClass {

    public SuperClass method1()
    {
       //do whatever
       return this
    }
}

public class SubClass extends SuperClass {

    //we inherit method 1

    //method2
    public SubClass method2()
    {
       //do whatever
       return this
    }
}

//succesfully returns instance of Sublass, but...
SubClass sc = new Subclass().method2() 

//...the following line returns an instance of SuperClass and not Sublass
//I want Sublass's instance, without having to using overides
//Is this possible?

SubClass sc = new Subclass().method1()

EDIT: —————————-usecase scenario——————————-

Message myMessage =  new ReverseTransactionMessageBuilder()
                    .policyNo(POLICY_NO) //on ReverseTransactionMessageBuilder
                    .audUserId(AUD_USER_ID) //on inherited MessageBuilder
                    .audDate(new Date()) //on inherited MessageBuilder
                    .processNo(EProcessConstants.FINANCE_MANUAL_ADJUSTMENT.getProcessCd()) //on inherited MessageBuilder
                    .serviceName("finance.ProcessReversalCmd") //on inherited MessageBuilder
                    .create(); //create is overridden so this is ReverseTransactionMessageBuilder

First thing youl notice is that sbrattla way allows me to call these .audDate () .xxx() methods in any order. With the class construct above you are forced to call the method on the sublcass last (or use a really ugly cast)

  • 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-25T17:20:45+00:00Added an answer on May 25, 2026 at 5:20 pm

    You would need to do something like:

    public class SuperClass<T> {
    
      public T method1() {
        return (T) this;
      }
    
    }
    
    public class SubClass extends SuperClass<SubClass> {
    
      public SubClass method2() {
        return (SubClass) this;
      }
    
    }
    

    You can read more about Java Generics in the “Generics Introduction“, but briefly explained you’re telling SuperClass to cast the returned instance to T which represents a type you define. In this case, it’s SubClass.

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

Sidebar

Related Questions

its hard for me to explain this one, but i hope some code will
Uhh Sorry its really hard to explain but. This is already a string: login_username=Username&login_password=Password&login_submit=Submit
Its' hard to explain what i mean so here an example. class mysql {
Its hard to explain so i have an example I have a left part
sorry for the confusing title, its really hard for me to explain what i
I know the title is a little off, but it's hard to explain the
This is really stumping me today. I'm sure its not that hard, but I
its a little bit hard to understand. in the header.php i have this code:
inHi, this question is fast, but from my point of view its pretty hard.
Ok this is hard to explain, but here goes. I have a 3D list

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.