Getting Around Bad Design Practices: Java Multiple Inheritance -
i working javafx shape subclasses, , have run believe rather unusual issue. goal extend several of these shape subclasses (i.e. rectangle, circle) in order add together own attributes these objects. example, extension of rectangle subclass this:
public class myrectangle extends javafx.scene.shape.rectangle implements specialinterface { private specialattributes specialattributes; // ... // constructors, getters , setters here // ... } where specialinterface can used specify methods related new attributes added myrectangle , mycircle, in case:
public interface specialinterface { public specialattributes getspecialattributes(); public void setspecialattributes(); } however, when seek create service classes reference these subclasses of rectangle , circle, seems though cannot generically. essentially, problem arises when need utilize attributes , methods both shape subclasses , specialinterface interface:
public class manipulationservice{ public manipulationservice(<undefined> myextendedshape) { // object javafx node, inherited javafx shapes (circle, rectangle, etc) myextendedshape.onrotate(new eventhandler<>(){ // ... }); // method myrectangle or mycircle myextendedshape.getspecialattributes(); } // ... } the issue here cannot create superclass extended shapes replace <undefined> above. specifically, if create superclass, cannot extend specific shapes want extend in subclasses due lack of multiple inheritance. if replace <undefined> shape, though, lose access methods in specialinterface.
i'm sure sort of multiple-inheritance problem has been solved before, cannot find solution. appreciate , suggestions on how handle situation.
you can define manipulationservice this:
class manipulationservice<t extends shape & specialinterface> { public manipulationservice(t myextendedshape) { // method shape myextendedshape.onrotate(/* ... */); // method specialinterface myextendedshape.getspecialattributes(); } } java inheritance javafx
No comments:
Post a Comment