Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Generics?  Covariant return types?

Thread view: 
Webb  Roberts - 10 Nov 2005 04:42 GMT
I'm having trouble ironing out language features in 1.5 to perform some
operations and have them be strongly typed.  I'd like to implement
strongly-typed classes derived from a simple base class.  It seems like
the result would be a combination of covariant return types, plus a
proper use of <T extends Number>, but I can't seem to get it right.

public abstract class Number {
    public abstract Number add(Number number);
}

The derived classes should override add(n), but be strongly typed.
Something that acts like what follows:

public class RealNumber extends Number {
    private final double real;
    public RealNumber(double real) {
        this.real = real;
    }
    public RealNumber add(RealNumber rhs) {
        return new RealNumber(this.real + rhs.real);
    }
}

But, obviously,  you'd want to be able to do operations in a proper
generic way, like on a Vector<T extends Number>.

Another example, for complex numbers:

public class ComplexNumber extends Number {
    private final double real;
    private final double imaginary;
    public RealNumber(double real, double imaginary) {
        this.real = real;
        this.imaginary = real;
    }
    public ComplexNumber add(ComplexNumber rhs) {
        return new ComplexNumber(this.real + rhs.real, this.imaginary +
rhs.imaginary);
    }
}
Roedy Green - 10 Nov 2005 04:47 GMT
>public class ComplexNumber extends Number {
>    private final double real;
>    private final double imaginary;
>    public RealNumber(double real, double imaginary) {
oops
           public ComplexNumber(double real, double imaginary) {
>        this.real = real;
>        this.imaginary = real;
>    }

What threw me off was you use of the name "Number" for your class
which already has meaning in Java.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Webb  Roberts - 10 Nov 2005 04:55 GMT
Yeah, that's a typo in the example.  obviously, it's just (wrong)
simple example code.  What I need help with is how to form the derived
types (RealNumber, ComplexNumber), so that the add() has the right
form.  Any help?

Thanks,
Webb
Thomas Hawtin - 10 Nov 2005 07:21 GMT
Webb Roberts wrote:
> I'm having trouble ironing out language features in 1.5 to perform some
> operations and have them be strongly typed.  I'd like to implement
[quoted text clipped - 5 lines]
>     public abstract Number add(Number number);
> }

It's generally not a good idea to reuse java.lang class names (or tabs).

You are trying to do much the same a java.lang.Comparable, so as it does
give Number a parameter, Number<T>. If you want to specify a bound, copy
Enum, so Number<N extends Number<N>>.

public abstract class NumberValue<N extends NumberValue<N>> {
    public abstract N add(N number);
}

public final class RealNumber extends NumberValue<RealNumber> {
    private final double real;
    public RealNumber(double real) {
        this.real = real;
    }
    public RealNumber add(RealNumber rhs) {
        return new RealNumber(this.real + rhs.real);
    }
}

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.