Note that there are some explanatory texts on larger screens.

plurals
  1. POOverride a method and make it behave static
    primarykey
    data
    text
    <p>I have an existing framework, which I can't change, it reads 2 properties</p> <ul> <li><code>ClassA=somepackage.AnImplementationOfInterfaceA</code></li> <li><code>ClassB=somepackage.AnImplementationOfInterfaceB</code></li> </ul> <p>Which calls <code>public methodA</code> on a <code>new ClassA()</code>, <code>public methodB</code> on a <code>new ClassB()</code> in that order</p> <p>I want to make a <code>class C</code> which implements interfaces A, B and provides hooks <code>methodC1</code>, <code>methodC2</code> for <code>class D</code> to override (<code>methodA</code> &amp; <code>methodB</code> contain a lot of boilerplate and complex to implement - <code>methodC1</code> &amp; <code>methodC2</code> would encapsulate the business logic) . Then my properties would be</p> <ul> <li><code>ClassA=somepackage.classD</code> </li> <li><code>ClassB=somepackage.classD</code></li> </ul> <p>The problem is that the person who implements class D might be tempted to write something like:</p> <pre><code>class D extends class C { private int foo; //must be non-static due to multi-threaded new Class D() calls going on int methodC1() {this.foo = read number from network} methodC2(int x) {y = this.foo;} //methodC2 is always called after methodC1 //methodA, methodB implementation inherited from C } </code></pre> <p>But this wouldn't work as expected since the framework would actually create a new object of <code>class D</code> each time before invoking <code>methodA</code>, <code>methodB</code> and thus can't rely on using the "this" reference.</p> <p>Defining <code>methodC1</code>, <code>methodC2</code> as <code>static</code> wouldn't work either because then the call to <code>methodC1</code> is tied to the implementation in <code>C</code>, not the overriden one in <code>D</code>. </p> <p>When really what ought to be written is:</p> <pre><code>class D extends class C { int methodC1() {return number from network;} methodC2(int x) {y = x} //here y is using the return value of methodC1 //methodA, methodB implementation inherited from C } </code></pre> <p>I would also like <em>only</em> <code>methodC1</code>, <code>methodC2</code> to be overridable i.e. programmers working on D can't mess with <code>methodA</code></p> <p>The ideal design would have</p> <ul> <li>the properties refer to one class only</li> <li><code>methodC1</code>, <code>methodC2</code> to be in that class</li> </ul> <p>Summary of challenges</p> <ul> <li>no <code>this</code> in <code>methodC1</code>, <code>methodC2</code></li> <li>can't make <code>methodC1</code>, <code>methodC2</code> <code>static</code></li> <li>properies takes only a instantiable class</li> </ul> <p>How do I design this framework? Is this even solvable? You can change the signature of <code>methodC1</code>, <code>methodC2</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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