Frage

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 " ";

 }
War es hilfreich?

Lösung 2

Override the method in your EmptySpace class:

public EmptySpace(Position position) {

    super(position);

}

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

Andere Tipps

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";
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top