0

I have three classes

public abstract class Champion { private String name; public Champion(String ChName) { name = ChName; } public void setName(String ChName) { name = ChName; } public String getName() { return name; } } 

second:

public class Mage extends Champion { public Mage(String ChName) { super(ChName); } public String toString() { return String.format("%s",super.toString()); } } 

and my main:

public class JavaApplication2 { public static void main(String[] args) { Mage mage = new Mage("ori"); System.out.println("champion is "+mage); } } 

The output should be "champion is ori" but I get: "champion is javaapplication2.Mage@1fee6fc"

What am I doing wrong?

2 Answers 2

8

You need to override toString() in Champion as the call to super.toString() in Mage.toString() will be calling Object.toString().

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Sir and sorry for your trouble. I shall remember to do this from now on.
@user1913592, no problem and apology unrequired.
0

By calling super.toString() you call the Object toString() method, giving you the result you see.

You need to implement the Champion toString() method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.