File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Mutually dependent generic abstract classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Mutually dependent generic abstract classes" Watch "Mutually dependent generic abstract classes" New topic
Author

Mutually dependent generic abstract classes

Lorenz Fischer
Greenhorn

Joined: Feb 16, 2007
Posts: 7
Hi there,

Maybe someone in here gets this generics topic thoroughly Even though I thought I did, I obviously don't :-/

Ok, here's the story:
I have two abstract classes that link to each other. I'd like to use generic types, so I can define some of the methods in my abstract classes while still having type safety when calling them from the subclasses. I tried it many ways, but I'm unable to write the code below without having either warnings about type safety, usage of raw types, or bound mismatch compilation errors.

Any help would be greatly appreciated :-)



Cheers
Lorenz
[ January 04, 2008: Message edited by: Lorenz Fischer ]

SCJP 1.5
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The problem is that a class declared as

class MyItem extends Item<Bid<MyOtherItem>>

would be syntactically legal, but lead to problems.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
The infinite recursion looks to be a problem for this new language feature.

I think you will have to pick one of the two types to genericize.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Lorenz Fischer
Greenhorn

Joined: Feb 16, 2007
Posts: 7
Thank you Ilja and Bill for your answers,

I tried it another way now. In the new code there can be no misunderstanding for the compiler, since one has to specify both (MyItem and MyBid) at the same time. But now I get an error stating that:

The method setItem(I) in Type Bid<I,B> is not applicable for the arguments (Item<I,B>

I don't understand this, since "I" in my case is defined as "something" that "extends Item<I,B>", which is what I'm trying to pass as the method argument. Do you spot the mistake?



Thank you very much for your time!
[ January 08, 2008: Message edited by: Lorenz Fischer ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mutually dependent generic abstract classes