JAVA MULTIPLE INHERITANCE

Samuel Fernandes

Total Post:159

Points:1117
Posted by  Samuel Fernandes
 859  View(s)
Ratings:
Rate this:

I am working with the JavaFX Shape subclasses, and I have run into what I believe is a rather strange issue. My goal is to extend several of these shape subclasses (i.e. RectangleCircle) in order to add my own attributes to these objects. For example, the extension of the Rectangle subclass would look like this:

public class MyRectangle extends javafx.scene.shape.Rectangle 
    implements SpecialInterface {

    private SpecialAttributes specialAttributes;
}

Where the SpecialInterface can be used to specify the methods related to the new attributes that will be added to MyRectangle and MyCircle, in this case:

public interface SpecialInterface {

    public SpecialAttributes getSpecialAttributes();
    public void setSpecialAttributes();

} 

However, when I try to create service classes that reference these subclasses of Rectangle and Circle, it seems as though I cannot do so generically. Essentially, the problem arises when I need to utilize attributes and methods from both the Shape subclasses and the SpecialInterface interface:

public class ManipulationService{

    public ManipulationService(<Undefined> myExtendedShape) {
        myExtendedShape.onRotate(new EventHandler<>(){
        });

        myExtendedShape.getSpecialAttributes();
    }

}

The issue here is that I cannot create a superclass for my extended shapes that would replace <Undefined> above. Specifically, if I create a superclass, I cannot extend the specific shapes that I want to extend in my subclasses due to a lack of multiple inheritance. If I replace <Undefined> with Shape, though, I then lose access to the methods in SpecialInterface.

I'm sure that this sort of multiple-inheritance problem has been solved before, but I cannot find the solution. I appreciate any and all suggestions on how to handle this situation.

  1. Simons Hood

    Post:70

    Points:494
    Re: Java Multiple Inheritance

    You can define ManipulationService like this:

    class ManipulationService<T extends Shape & SpecialInterface> {
        public ManipulationService(T myExtendedShape) {
            myExtendedShape.onRotate(/* ... */);
            myExtendedShape.getSpecialAttributes();
        }
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!