Reputation: 322
I want to override the superclass's method in my subclass. But no matter what change I made to my programs. It is still indicating the result that was proceed by the superclass's method. So, what's wrong in my codes?
I want to override the getChildAge method and getGreeting method. It should display 14 (if 12 is being set)after overriding, which is simply reporting actual age plus 2. For the getGreeting method, it should display "I am the best." no matter what argument was passed into the getGreeting method.
Here is my code:
public class SchoolKid
{
private String childName;
private int childAge;
private String childTeacher;
private String greeting;
//Constructor sets the childName,age,childTeacher and greeting.
public SchoolKid(String childN,int a,String childT,String g)
{
childName = childN;
childAge = a;
childTeacher = childT;
greeting = g;
}
public void setChildName(String childN)
{
childName = childN;
}
public void setChildAge(int a)
{
childAge = a;
}
public void setChildTeacher(String childT)
{
childTeacher = childT;
}
public void setGreeting(String g)
{
greeting = g;
}
public String getChildName()
{
return childName;
}
public int getChildAge()
{
return childAge;
}
public String getChildTeacher()
{
return childTeacher;
}
public String getGreeting()
{
return greeting;
}
}
public class ExaggeratingKid extends SchoolKid
{
private int childAge;
private String greeting;
//Constructor sets the child name,age,childTeacher and greeting.
public ExaggeratingKid(String childNAME,int childAGE,
String childTEACHER,String GREETING)
{
super(childNAME,childAGE,childTEACHER,GREETING);
}
public void setChildAge(int a)
{
childAge = a;
super.setChildAge(childAge+2);
}
public void setGreeting(String g)
{
greeting = "I am the best.";
super.setGreeting("I am the best.");
}
public String toString()
{
String str;
str = "The child's name is "+super.getChildName()+
"\nThe child's age is "+super.getChildAge()+
"\nThe child's teacher is "+super.getChildTeacher()+
"\nThe greeting is: "+super.getGreeting();
return str;
}
}
Upvotes: 1
Views: 9155
Reputation: 285403
What good is overriding if you call the super method? Don't call the super
methods in toString()
! Call the methods of the current object, the this
object. Also, don't give child a greeting or childAge field.
For e.g., this makes no sense:
public void setChildAge(int a)
{
childAge = a;
super.setChildAge(childAge+2);
}
Instead just do:
@Override
public void setChildAge(int a)
{
// childAge = a; // the child shouldn't have this variable
super.setChildAge(a + 2);
}
Likewise this is no good:
public void setGreeting(String g)
{
greeting = "I am the best.";
super.setGreeting("I am the best.");
}
Instead simply do:
@Override
public void setGreeting(String g)
{
// but what is g for???
// greeting = "I am the best.";
super.setGreeting("I am the best.");
}
Or perhaps better:
@Override
public String getGreeting() {
return "I am the best.";
}
and get rid of the greeting and childAge fields in the child class. But if you do this, you must call this in the child's constructor to be sure that the greeting is set.
Upvotes: 4
Reputation: 13556
Here is your problem
public String toString()
{
String str;
str = "The child's name is "+super.getChildName()+
"\nThe child's age is "+super.getChildAge()+
"\nThe child's teacher is "+super.getChildTeacher()+
"\nThe greeting is: "+super.getGreeting();
return str;
}
every time you are using super
members. just remove the super
keyword from this method
public String toString()
{
String str;
str = "The child's name is "+ getChildName()+
"\nThe child's age is "+ getChildAge()+
"\nThe child's teacher is "+ getChildTeacher()+
"\nThe greeting is: "+ getGreeting();
return str;
}
Upvotes: 1