Note that there are some explanatory texts on larger screens.

plurals
  1. POparent class to return subclass
    text
    copied!<p>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 <code>SubClass sc = new Subclass().method1()</code> line to return the Subclass instance.</p> <pre><code>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() </code></pre> <p><b>EDIT: ----------------------------usecase scenario-------------------------------</b> </p> <pre><code>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 </code></pre> <p>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)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload