Вопрос

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