Reputation: 514
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)
As you can see with the red arrows I would need to extend from two classes.
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.
Upvotes: 2
Views: 112
Reputation: 1462
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.
Upvotes: 3
Reputation: 665
Use Interface
because java is not supports Multiple Inheritance
. Interfaces are abstract types that specify behaviors for classes to implement. Because they are abstract, interfaces do not contain detailed instructions for their behaviors. Instead, the classes provide concrete implementations of interface behaviors.
Interfaces have many defining characteristics:
Unlike classes, you don't instantiate interfaces. Instead, classes implement interfaces
Interfaces contain only public constant definitions and method headers
Interfaces can only extend other interfaces, not classes
Interfaces can extend multiple interfaces, and classes can implement multiple interfaces
Upvotes: 0