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 / January 2007

Tip: Looking for answers? Try searching our database.

Implicit type parameters

Thread view: 
SoulSpirit@gmail.com - 30 Jan 2007 14:27 GMT
Let's suppose we have an abstract class who wants to force its
subclasses to implement Comparable in a way that the extending class
has the ability to be compared only to objects of its same type.
The solution I can figure out is the following:

public abstract class Animal<T extends Animal> implements
Comparable<T>{
    public abstract int compareTo( T other );
}

public class Dog extends Animal<Dog>{
    public int compareTo( Dog other ){
        return 0;
    }
}

But it makes me doubtful about the necessity to declare the extending
class with a type parameter (Dog) that is the same as the class name.
I feel like I could eliminate the repetition (Dog-Dog) changing
something in Animal declaration, obtaining something like this:

public class Dog extends Animal{ //Notice, there isn't <Dog>
    public int compareTo( Dog other ){
        return 0;
    }
}

Is it possible?
Thanks
Ingo R. Homann - 30 Jan 2007 14:46 GMT
Hi,

> Is it possible?

No.

IMHO, this has something to do with the Liskov-substitution-principle:
Wherever you have a reference to an (not-generic) Animal, it should be
possible to compare it with another Animal. This should be of course
also true, if the Object to which the reference refers is of type Dog.

I think, your first solution is the correct one.

Ciao,
Ingo
SoulSpirit@gmail.com - 30 Jan 2007 15:21 GMT
> Wherever you have a reference to an (not-generic) Animal, it should be
> possible to compare it with another Animal. This should be of course
> also true, if the Object to which the reference refers is of type Dog.

Thanks for your answer.
I can't disagree with your point of view, but we know real-world
applications are always an exception to every principle :)
In the simplified example i've omitted the purpose of my... dilemma.

The purpose is to give someone other a clear class to extends that
already do all work but specialized jobs, giving less space as
possible to the implementor.
In short, my superclass is always able to compare an Animal with
another one if they are of different type, but fine comparision is
needed when the two Animal(s) are of the same type. The real
superclass is much like this:

public abstract class Animal<T extends Animal> implements
Comparable<T>{
    abstract int compareToInternal( T other );

    public final int compareTo( T other ){
        if( getClass() == other.getClass() )
            return compareToInternal( other );
        else
            ... Animal knows what to do here ...
    }
}

And I'd like to force subclasses to implement this compareToInternal()
method that compares only instances of the same type (Dogs with Dogs
and Cats with Cats).

An implementation like this...

public class Dog extends Animal<Cat>{
    int compareToInternal( Cat other ){
        return 0;
    }
}

... should be seen as an error by the compiler.

Bye
Eric Sosman - 30 Jan 2007 16:33 GMT
SoulSpirit@gmail.com wrote On 01/30/07 10:21,:
> [...]
> And I'd like to force subclasses to implement this compareToInternal()
[quoted text clipped - 10 lines]
>
> ... should be seen as an error by the compiler.

   I don't think you can do that.  An abstract class (or an
interface) can require its subclasses (implementors) to provide
methods with particular signatures, but it cannot prevent them
from providing other methods as well.  Those other methods may
have the same names as some of the required methods (that's
called "overloading"), but they are distinct methods.  Java
will allow the subclass to provide all of

    int compareToInternal(String attached) { ... }
    int compareToInternal(double trouble) { ... }
    int compareToInternal(char broiled) { ... }
    int compareToInternal(float aLoan) { ... }
    int compareToInternal(int p, int q, int r) { ... }

... and there's nothing (as far as I know) you can do to
prevent it.

Signature

Eric.Sosman@sun.com



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.