Reputation: 1515
I have an abstract class like this
public abstract class Temperature
{
private float value;
public Temperature(float v)
{
value = v;
}
public final float getValue()
{
return value;
}
public abstract Temperature toCelsius();
public abstract Temperature toFahrenheit();
public abstract Temperature toKelvin();
}
then I have classes that extend this Temperature class, example:
public class Celsius extends Temperature
{
public Celsius(float t)
{
super(t);
}
public String toString()
{
return "";
}
@Override
public Temperature toCelsius() {
// TODO Auto-generated method stub
return this;
}
public Temperature toKelvin(){
return new Kelvin(this.getValue() + 273);
}
@Override
public Temperature toFahrenheit() {
// TODO Auto-generated method stub
return new Fahrenheit(this.getValue() * 9 / 5 +32);
}
}
main method creates objects of of Celcius
Temperature inputTemp = null, outputTemp = null;
inputTemp = new Celsius(temp_val);
outputTemp = inputTemp.toCelsius();
then prints the object by calling this method
System.out.println("\n The converted temperature is " + outputTemp.toString() +"\n\n");
}
What do i have to put in the toString method in order to print the desired value? this.super.getValue() didnt work and im kinda clueless. Since we are not going to be returning the same object everytime, dont we have to use the superclass?
Upvotes: 0
Views: 182
Reputation: 692003
this.super
is invalid syntax. super
is not a field of this
. It's a keyword that allows calling the superclass implementation of a method rather than calling the overridden implementation, from the current class. You just need
return Float.toString(this.getValue());
or
return Float.toString(getValue());
or even
return Float.toString(super.getValue());
But using super.getValue()
is useless, since the subclass doesn't override the base getValue()
method, and you thus don't need to explicitely use the super implementation of the method.
Upvotes: 0
Reputation: 43023
It will be enough if you use:
public String toString()
{
return Float.toString(this.getValue());
}
Upvotes: 1