Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Protected Method Before Interface
    primarykey
    data
    text
    <p>I have to design a datastructure that'll get implemented multiple times, and I've struck upon a problem.</p> <p>Since my datastructure needs to have multiple versions of it, I've created an abstract class that lays the groundwork for all implementations. However, the datastructure requires a set-view of certain parts too.</p> <p>The problem becomes as follows: The set needs to have different implementations depending on the implementation of my datastructure: Either the HashSet or the Collections.SingletonSet. The two implementing datastructures will then extend bits of these to do additional tasks when items are added or removed from the set. However, the abstract datastructure also requires a way to internally remove elements from this set such that this extra work isn't done. For this I'd like to add a protected method to the set, but I can't!</p> <p>To illustrate, here's some sample code relating to the type of datastructure I'm creating:</p> <pre><code>public abstract class AbstractEdge { public abstract AbstractSetView destination(); //Gives a subclass of AbstractSetView in implementations. public void doStuff() { destination().removeInternal(foo); } public abstract class AbstractSetView implements Set&lt;Vertex&gt; { protected abstract void removeInternal(Vertex vert); } } public class Edge extends AbstractEdge { public SetView destination() { return new SetView(); } public class SetView extends AbstractSetView,Collections.SingletonSet&lt;Vertex&gt; { //Doesn't work this way. protected void removeInternal(Vertex vert) { //Do stuff. } } } public class HyperEdge extends AbstractEdge { public SetView destination() { return new SetView(); } public class SetView extends AbstractSetView,HashSet&lt;Vertex&gt; { //Doesn't work this way. protected void removeInternal(Vertex vert) { //Do stuff. } } } </code></pre> <p>These are the options I've considered:</p> <ul> <li>As above, extending from multiple classes isn't allowed.</li> <li>Making the AbstractSetView an interface causes the removeInternal() method to become public, which is undesirable.</li> <li>Making SetView extend only AbstractSetView and implement everything myself... twice. But this requires me to basically include HashSet and SingletonSet implementations, as an inner class, which is extremely ugly.</li> </ul> <p>Surely the designers of Java made some way around this, to enable me to use their built-in Set implementations? What am I overlooking?</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