Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Alternative to Multiple Inheritance to Extend Library

I’m currently trying to extend WPILib. Unfortunately I would need multiple inheritance.

I’ve drawn a uml diagram of the problem (forgive me my crappy handwriting)
enter image description here

As you can see with the red arrows I would need to extend from two classes.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I can’t change any thing inside the WPI box and the requires method from ICommand needs some functions from CommandBase so I can’t make it default in ICommand.

How can I solve this?

If you want to try some thing out here’s my github repo.

>Solution :

If I understood correctly, you want to have your custom classes (SequentialCommandGroup, ParallelCommandGroup) to extend from library classes, but also to have your own implementation for that "Command" part.

You cannot have multiple inheritance in Java of course, but I think you could utilize Proxy pattern here.

Your classes would still inherit from Library classes, but the implementation of ICommand interface could be delegated to Command object.

Example:

public class MyParallelCommandGroup extends ParallelCommandGroup implements ICommand {

    private Command commandProxy;
    
    public MyParallelCommandGroup() {
        // instantiate commandProxy here, or inject it as constructor param
    }
    
    @Override
    public void requires() {
        commandProxy.requires();
    }
    
}

You could even extract whole Command class to an interface and implement it in your proxy class. I’m not sure whether this covers your use-case, but maybe it will help you to find a proper solution.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading