For some derived classes, I want to ensure that one of two overloaded abstract methods gets overridden, but not both. Is this possible?
abstract void move();
abstract void move(int x, int y);
There is an abstract particle class that is extended by several clas开发者_运维百科ses. One of the derived classes receives mouse input to calculate its movement, while the others do not. All of the derived classes have a move function. What is a good way to go about coding the inheritance for this?
No. But if you explain why you want to do it then may be there are other options.
No, this is not possible, as it would break polymorphism rules. You should rethink your architecture, as this is severe code smell.
No, Java requires that all abstract
methods be implemented by concrete subclasses.
Leave the decision of whether the parameters should be ignored to the callee, not the caller. Take for example a hypothetical 3D rendering system with multiple rendering engines:
abstract class Renderer {
boolean isPointVisible(int x, int y);
}
class SimpleRenderer {
@Override
public boolean isPointVisible(int x, int y) {
return true;
}
}
class ComplexRenderer {
@Override
public boolean isPointVisible(int x, int y) {
return x > 0 && x < 100 && y < 0 && y < 100;
}
}
Given a number of strategies can alter the state of the particle it seems the responsibility of moving it should be external to the particle itself. It seems like the particle should itself be a simple concrete class which holds state information (i.e. position, etc), whilst at runtime you can determine which strategy will operate on changing the particle's state.
精彩评论