Hi Forum,
I have written an abstract class called 'Field', which within has an abstract method called 'getValue()'. So, so far I have:-
public abstract class Field
{
// instance variables
private double x;
private double y;
public Field(double x)
{
this.x = x;
}
public Field(double x, double y)
{
this.x = x;
this.y = y;
}
abstract double getValue();
}
Now, I have two subclasses of Field, called 'Real' and 'Complex', which both have concrete implementations of the method 'getValue()'. So, they both look like:-
public class Real extends Field
{
// instance variables
private double x;
public Real(double a)
{
super(a);
}
public double getValue()
{
return(this.x);
}
}
And, 'Complex' looks like:-
public class Complex extends Field
{
// instance variables
private double x;
private double y;
public Complex(double a, double b)
{
super(a, b);
}
public double getValue()
{
return(0.0);
}
}
Now, my problem is, is that I want the version of 'getValue()' in the Complex class to have a different return type than 'double'.Possibly for it to have either a 'void' or an error message.
I have tried messing around with generics, and also 'covariant return types', but cannot seem to get either to work without the compiler complaining. Any ideas appreciated.


LinkBack URL
About LinkBacks
Reply With Quote