Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get the benefits of implementation inheritance without tying my class to a particular implementation?
    primarykey
    data
    text
    <p>I'm developing an application which builds on a class written by another developer (for which I do not have the source).</p> <p>I wish to use all of the functionality of said class but also to extend it with additional functionality. Ordinarily to achieve this I would have defined an interface (<em>MyInterface</em>) and have extended the external class (<em>TheirClass</em>) from my own (<em>MyClass</em>) while implementing <em>MyInterface</em>.</p> <pre class="lang-java prettyprint-override"><code>public interface TheirClassInterface { public void theirMethod1(); public void theirMethod2(); } public class TheirClass implements TheirClassInterface { public void theirMethod1() { ... } public void theirMethod2() { ... } } public class TheirOtherClass { public void theirOtherMethod1(TheirClassInterface o) { ... } } public interface MyInterface() { public void myMethod1(); } public class MyClass extends TheirClass implements MyInterface { public void myMethod1() { ... } } public class MyNewClass extends MyClass { public void MyNewClassMethod() { ... } } </code></pre> <p>The problem is complicated by the fact that:</p> <ol> <li>I now wish to create a new class (<i>MyNewClass</i>) which adds additional functionality to <i>MyClass</i> but I don't want my code to be dependent on <i>TheirClass</i>. <li>I wish to be able to use my class as a parameter to the method of <i>TheirOtherClass</i>. </ol> <p>To combat this I refactored my code to instead use composition over inheritance and implementing <em>TheirClassInterface</em>. This works but requires me to implement many methods and delegate them to <em>theirClassObject</em> (in reality <em>TheirClassInterface</em> contains a very large number of methods).</p> <pre class="lang-java prettyprint-override"><code>public interface TheirClassInterface { public void theirMethod1(); public void theirMethod2(); } public class TheirClass implements TheirClassInterface { public void theirMethod1() { ... } public void theirMethod2() { ... } } public class TheirOtherClass { public void theirOtherMethod1(TheirClassInterface o) { ... } } public interface MyInterface() { public void myMethod1(); } public class MyClass implements TheirClassInterface, MyInterface { private TheirClass theirClassObject; public void myMethod1() { ... } public void theirMethod1() { theirClassObject.theirMethod1(); } public void theirMethod2() { theirClassObject.theirMethod2(); } } public class MyNewClass extends MyClass { public void MyNewClassMethod() { ... } } </code></pre> <p>My question is whether my approach is appropriate in this case and whether it could be improved upon as it seems to me that my code uses an excessive amount of delegation to get the job done.</p> <p>Many thanks for any guidance anyone can give on this.</p> <p>Danny</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.
 

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