Method problem

I have 2 similar classes called

Number(integers) and Real(doubles)

I have another class called Arithmetic in which there are arithmetic functions like "plus,minus,division,multiplication"

In the Arithmetic class i have this method of addition, where if the numbers are taken and if they are integers it returns an integer value or if they are doubles, it returns real:

public Number plus(Environment env)throws LispException{

if (this.isNull() && this.numberP())returnnew Number(0);

if (this.isNull() && this.realP())returnnew Real(0);

SExpression carResult = this.car().eval(env);

if (carResult.numberP() )

{

returnnew Number(((Number)carResult).intValue() + this.cdr().plus(env).intValue());

}

elseif (carResult.realP() )

{

returnnew Real(((Real)carResult).realValue() + this.cdr().plus(env).realValue());

}

}

My problem is that when i call this method by

"public Number plus(Environment env) "

in line 3 it gives me incompatible type of "Real" because it expects a "Number", This occurs wherever i have Real types inside this method.

Is there a way to solve this problem? i was thinking of casting, but how would i do that..

Thanks

[1949 byte] By [RelCoa] at [2007-11-27 2:24:25]
# 1
I am guessing Real and Number are your own classes.A method can only return one type. That could include children of a type.So in your case you would need a base class for both of the types you have. Presuming that you intend for Number to represent only integers.
jschella at 2007-7-12 2:31:26 > top of Java-index,Java Essentials,Java Programming...