I'm calling a method from the Superclass called getStringRepresentation which currently returns " ". Which is fine. My problem is I want to change the return statement in my Subclass to another symbol such as S or O for example.

Currently my code looks like this:

public EmptySpace(Position position) {
    super(position);
    super.getStringRepresentation()
        return "ES"; //this obviously doesn't work.
}

I understand with a string you can just override it with system.out, but the java tutorials don't explain or have an example for return statements. but the java tutorials don't explain or have an example for return statements.

Main class

    public class Sector
    {
    private Position            position;

 /**
 * Returns a String containing " " (Whitespace).
 * 
 * @Return returns a String containg " "
 */
 public String getStringRepresentation() {

    return " ";

 }
有帮助吗?

解决方案 2

Override the method in your EmptySpace class:

public EmptySpace(Position position) {

    super(position);

}

@Override
public String getStringRepresentation() {
    return "ES";
}

其他提示

Don't call the getStringRepresentation() method from the constructor of subclass.

Instead override the getStringRepresentation() method in your subclass.

public EmptySpace(Position position) {
   super(position);
}

@Override
public String getStringRepresentation() {
  return "ES";
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top