user2535970
user2535970

Reputation:

Java calling method from Superclass and changing the return statement

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

 }

Upvotes: 1

Views: 690

Answers (2)

Arjit
Arjit

Reputation: 3456

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

Upvotes: 1

Roberto
Roberto

Reputation: 9090

Override the method in your EmptySpace class:

public EmptySpace(Position position) {

    super(position);

}

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

Upvotes: 0

Related Questions