There is a class with inner classes:

public class ParentClass { //состояние объекта public class State { public void action(){} } public State currentState; public void setState(int stateID) { if(stateID==0) currentState = new Born(); if(stateID==1) currentState = new Life(); if(stateID==2) currentState = new Dead(); } //виды состояний public class Born extends State { @Overide public void action(){} } public class Life extends State { @Overide public void action(){} } public class Dead extends State {} @Overide public void action(){} } ///уделяем объекту внимание public void Action (){ currentState.action(); } } 

How to override in the successor the methods of the inner classes Born Life and Dead, without inheriting the classes themselves?

 Задумка была простой 1. Обеспечить каждого наследника своей реализацией методов action(). 2. Оставить метод переключений состояния объекта setState() в родителе. 

    3 answers 3

    If I understood correctly, there was something like this:

     public class ParentClass { public State currentState; public void setState(int stateID) { if (stateID == 0) currentState = new Born() { @Override public void action() { // action for Born state } }; if (stateID == 1) currentState = new Life() { @Override public void action() { // action for Life state } }; if (stateID == 2) currentState = new Dead() { @Override public void action() { // action for Dead state } }; } //состояние объекта public class State { public void action() { } } //виды состояний public class Born extends State { } public class Life extends State { } public class Dead extends State { ///уделяем объекту внимание public void Action() { currentState.action(); } } } 

    Or in general:

     public class ParentClass { public State currentState; public void setState(int stateID) { if (stateID == 0) currentState = new State() { @Override public void action() { // action for Born state } }; if (stateID == 1) currentState = new State() { @Override public void action() { // action for Life state } }; if (stateID == 2) currentState = new State() { @Override public void action() { // action for Dead state } }; } ///уделяем объекту внимание public void Action() { currentState.action(); } //состояние объекта public class State { public void action() { } } } 

    But in general, the State pattern is a little differently better implemented.

    • You also inherit;) - Sergey Gornostaev
    • @SergeyGornostaev added to the response without inheritance at all. But I think the author was referring to something else ... - Oleksiy Morenets
    • the idea was this: setState () is left unchanged in the parent, so we know all its known states. (Born Life Dead). And the implementation of the methods of these states give to the heirs. - FatherOfFiveChildren
    • In your options, the setState () method will have to be redefined as a successor, but I find the solution very interesting. - FatherOfFiveChildren
    • Thank. How would you implement the State pattern? - FatherOfFiveChildren
     public class ChildClass extends ParentClass { class ChildInner extends ParentClass.ClassBorn { public void action() {} } } 

    And in your example, not local classes, but internal ones.

    • Is it possible to redefine methods of inner classes without inheriting the inner classes themselves? - FatherOfFiveChildren
    • No impossible. - Sergey Gornostaev
    • Maybe I didn’t put the problem right, I added a question - FatherOfFiveChildren
    • My answer does not change from this; you cannot override the methods of nested classes without inheriting them. - Sergey Gornostaev
    • @SergeyGornostaev, so they asked without inheritance ... - Oleksiy Morenets

    How I would implement the State pattern

     public interface MotorState { void on(Motor motor); void off(Motor motor); } 

    2 states, for example, on and off:

     public class MotorOn implements MotorState { @Override public void on(Motor motor) { System.out.println("Motor is already on"); } @Override public void off(Motor motor) { motor.setState(new MotorOff()); System.out.println("Motor is off"); } @Override public String toString() { return "Motor is on"; } } public class MotorOff implements MotorState { @Override public void on(Motor motor) { motor.setState(new MotorOn()); System.out.println("Motor is on"); } @Override public void off(Motor motor) { System.out.println("Motor is already off"); } @Override public String toString() { return "Motor is off"; } } 

    Motor itself:

     public class Motor { private MotorState state; public Motor() { this.state = new MotorOff(); } public MotorState getState() { return state; } public void setState(MotorState newState) { this.state = newState; } public void turnOn() { state.on(this); } public void turnOff() { state.off(this); } public boolean isOn() { return state instanceof MotorOn; } } 
    • thank. Interesting. - FatherOfFiveChildren