
generics and inheritance
Hi, I was searching a bit within the forums but couldn't quit find what I was looking for, hopefully you guys can help me anyway :)
For a school project I am required to create a Class that handles fraction numbers. Which is in it self not that complicated and I got it working pretty quickly. Anyway what I wonted to do now, was to create a abstract super class called Number that would require any inheritor to have a few methods like: add, sub, div and so on.
So here is what i did:
Code:
public abstract class Number extends java.lang.Number {
public abstract <T extends Number> T add(T a,T b);
public abstract <T extends Number> T sub(T a,T b);
}
The problem however it that I wonted the inheritors to look like this:
Code:
public class Fraction extends Number {
public Fraction add(Fraction a, Fraction b) ...
public Fraction mult(int value, Fraction f) ...
}
but instead i have to write like this to get correct code:
Code:
public class Fraction extends Number {
@Override
public <T extends Number> T add(T a, T b) ...
@Override
public <T extends Number> T sub(T a, T b) ...
}
Is the result I wont possible? I tired various methods so far without any luck. Thanks for your help ;)

I think that you want to declare your generics on the class definition line:
Code:
abstract class MyNumber<T extends Number> {
public abstract T add(T t1, T t2);
public abstract T sub(T t1, T t2);
}
I doubt that you want this class to truly extend Number, but if so, please let me know your thinking as to why.
Also, it's very confusing to name your class Number and also use java.lang.Number in your code.

First of all thanks for your quick response ;)
Regarding my poor choice for a name I have to agree with you, for the moment you can use this class body instead of the one from my first post:
Code:
public abstract class MyNumber...
to inherit the methods of java.lang.Number is actually not needed jet. My main problem was that I wonted to have each inheritor of MyNumber to define methods like add only on them self like this.
For my fractal Numbers:
Code:
public Fraction add(Fraction a, Fraction b)...
For some other inheritor:
Code:
public Inheritor add(Inheritor a, Inheritor b)
I hope you get where I am going with this, but as far as i read on the web there is not really a way to do this with Java?

Ok i think i found a solution to my problem:
the MyNumber class:
Code:
public abstract class MyNumber<T extends MyNumber> {
public abstract T add(T a,T b);
}
the Fraction class:
Code:
public class Fraction extends com.autoex.model.math.Number<Fraction> {
@Override
public Fraction add(Fraction a, Fraction b) ...
}
Is this a good way to do it or is there a better way to do it?

The prinicple looks OK, but shouldn't the Fraction class extend the MyNumber class instead of com.autoex.model.math.Number?
The MyNumber class doesn't seem to have any data or method implementations, so you could consider making it an interface.

You are completely right ;) it should have been com.autoex.model.math.MyNumber ;) I did forgot to chage it after renaming my Number class :P and i guess making it an interface makes even more sence ;) thanks for the tip