Reputation: 2572
I have three classes named Human.java, Superhero.java and Run.java. The Superhero extends Human and the method introduce() is overridden in Superhero with call to parent class's introduce(). But, When I'm making a superhero object and calling the introduce method, it doesn't print the base class method's returned value. What's wrong? Thanks in advance.
Human.java
public class Human implements Comparable<Human> {
private int age;
private String name;
public Human(String givenName, int age) {
this.name = givenName;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String introduce() {
return "Hey! I'm " + name + " and I'm " + age + " years old.";
}
@Override
public int compareTo(Human H1) {
if(this.getAge() > H1.getAge())
return 1;
else if (this.getAge() < H1.getAge())
return -1;
else
return 0;
}
}
Superhero.java
public class Superhero extends Human {
private String alterEgo;
private int age;
private String name;
public Superhero(String givenName, int age, String alterEgo) {
super(givenName, age);
this.alterEgo = alterEgo;
}
public String getAlterEgo() {
return alterEgo;
}
@Override
public String introduce(){
super.introduce();
return "I am also known as " + alterEgo + "!";
}
}
Run.java
public class Run {
public static void main(String[] args) {
Superhero superhero = new Superhero("Bruce", 26, "Batman");
System.out.println(superhero.introduce());
}
}
Upvotes: 3
Views: 91
Reputation: 7285
1st: You are calling super.introduce() which returns a string but you are not doing anything with that string. You need to assign it to a variable and add it to your return statement for it to be visible.
2nd: I recommend you change the introduce() method to toString() since that way you can get the string by just writing:
System.out.println(superhero);
Here is what you need to do to return the "Hey! I'm " + name + " and I'm " + age + " years old." part as well:
@Override
public String toString(){
return super.introduce() + "\n" + "I am also known as" + alterEgo + "!";
}
Personally i prefer implementing my toString() methods like this.
@Override
public String toString(){
String string = super.toString();
string = string + "\n";
string = string + "I am also known as";
string = string + alterEgo;
string = string + "!";
return string;
}
Upvotes: 2
Reputation: 17132
@Override
public String introduce(){
super.introduce();
return "I am also known as " + alterEgo + "!";
}
The introduce method of the SuperHero class is dumping the String returned by the call to the base method & returning "I am also known as " + alterEgo + "!"
instead.
You need to return the result of the concatenation of the String
returned by the base class's implementation + the SuperHero
-specific string:
@Override
public String introduce(){
return super.introduce() + "I am also known as " + alterEgo + "!";
}
Upvotes: 5
Reputation: 726519
The reason your method does not print what's returned from the superclass is that your overriding method drops the return value. Generally, this is OK, so Java does not warn you about that. However, in this case you want to use the return value, so you should not ignore the result of calling the super.introduce()
method.
Your method should take that value, and append to it, like this:
@Override
public String introduce(){
return super.introduce() + "\n"
+ "I am also known as " + alterEgo + "!";
}
Upvotes: 2