Force SubClasses to @Override method from SuperClass. Method in SuperClass must have body

核能气质少年 提交于 2020-01-03 20:35:39

问题


I would like to force SubClasses to @Override method from SuperClass.

Method in SuperClass can't be abstract, cause I wanna provide some basic implementation.

Here is example of my code:

public abstract class GenericModel<T extends GenericModel> {
    long id, counter;

    public String methodToBeOverridenInSubClass(String name) {
        // some basic implementation
        // rest details will be provided by subclass
        return name;
    }

}
public class SubClass extends GenericModel<SubClass> {
    @Override
    public String methodToBeOverridenInSubClass(String name) {
        switch (name) {
            case "name": return "Real name";
            default: super.methodToBeOverridenInSubClass(name);
        }
    }
}

This post is interesting: MustOverrideException and MethodNotOverridenException

Please give me some help


回答1:


Maybe what your are looking for is resolved by Template method pattern. In which you create a parent class with one method implemented which describe a process that calls anothers methods (steps). These steps are abstract so they are implemented by a sub class.

Example from wikipedia:

/**
* An abstract class that is common to several games in
* which players play against the others, but only one is
* playing at a given time.
*/

abstract class Game {

protected int playersCount;
abstract void initializeGame();
abstract void makePlay(int player);
abstract boolean endOfGame();
abstract void printWinner();

/* A template method : */
public final void playOneGame(int playersCount) {
    this.playersCount = playersCount;
    initializeGame();
    int j = 0;
    while (!endOfGame()) {
        makePlay(j);
        j = (j + 1) % playersCount;
    }
    printWinner();
}
}

//Now we can extend this class in order 
//to implement actual games:

class Monopoly extends Game {

/* Implementation of necessary concrete methods */
void initializeGame() {
    // Initialize players
    // Initialize money
}
void makePlay(int player) {
    // Process one turn of player
}
boolean endOfGame() {
    // Return true if game is over 
    // according to Monopoly rules
}
void printWinner() {
    // Display who won
}
/* Specific declarations for the Monopoly game. */

// ...
}

class Chess extends Game {

/* Implementation of necessary concrete methods */
void initializeGame() {
    // Initialize players
    // Put the pieces on the board
}
void makePlay(int player) {
    // Process a turn for the player
}
boolean endOfGame() {
    // Return true if in Checkmate or 
    // Stalemate has been reached
}
void printWinner() {
    // Display the winning player
}
/* Specific declarations for the chess game. */

// ...
}



回答2:


You can use two methods:

public abstract String methodToBeOverridenInSubClass(String name);


protected String commonMethodToUseInSubClasses(String name) {
    // some basic implementation
    return name;
}

This way the subclass must override methodToBeOverridenInSubClass but you can still use common code for all sub-classes in commonMethodToUseInSubClasses.



来源:https://stackoverflow.com/questions/31020048/force-subclasses-to-override-method-from-superclass-method-in-superclass-must

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!