wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Making Class Generic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Making Class Generic" Watch "Making Class Generic" New topic
Author

Making Class Generic

Devin Henderson
Greenhorn

Joined: Dec 01, 2009
Posts: 16
Hey all.

Last lab of the semester...thank God!

Anyways, our last lab is about generic classes and how to make classes generic.

Having a bit of a problem turning one of the given classes into a generic.

Here is the class:




In the class that is calling the addItem method, here the code:



Here is the ShortList addItem code:



The error I am getting in the ShortUnorderedList when I hover the "public <T> void addItem (T oneItem)":

Name clash: The method addItem(T) of type ShortUnorderedList<T> has the same erasure as addItem(Object) of type ShortList but does not override it

I know that whatever I am doing wrong is very basic. But, heck if I can figure it out. Any help would be appreciated!

Thanks!
Devin Henderson
Greenhorn

Joined: Dec 01, 2009
Posts: 16
Also, on L17 of the myGenericsTest class, there is the following error when you hover over the "newShortUnorderedList<Address>();":

Cannot instantiate the type ShortUnorderedList<Address>
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Devin Henderson wrote:Also, on L17 of the myGenericsTest class, there is the following error when you hover over the "newShortUnorderedList<Address>();":

Cannot instantiate the type ShortUnorderedList<Address>


You can't instantiate an abstract class (see line 1 of your first class).

John.
Devin Henderson
Greenhorn

Joined: Dec 01, 2009
Posts: 16
Well, I got the ShortUnorderedList class to stop giving me errors.

I am still however having issues with the instantiation on L17.

Any help would be appreciated!
Devin Henderson
Greenhorn

Joined: Dec 01, 2009
Posts: 16
John de Michele wrote:
Devin Henderson wrote:Also, on L17 of the myGenericsTest class, there is the following error when you hover over the "newShortUnorderedList<Address>();":

Cannot instantiate the type ShortUnorderedList<Address>


You can't instantiate an abstract class (see line 1 of your first class).

John.



Hello John.

This is the error I get if I remove the abstract from the class header:

The type ShortUnorderedList<T> must implement the inherited abstract method ShortList.addItem(Comparable)

So, I am assuming that the ShortList is passing the parameter to the ShortUnorderedList.addItem method in form of a Comparable. I thought that the "T" generic would handle that.

Apparently I am wrong.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Devin:

Can you post the code for your ShortList class?

John.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

1) The T in your addItem method is not the same T that is used for the entire class. You use something called a generic method here. Remove the <T> at the start.

2) ShortList is generic as well (as can be seen from the method signature), so you must extend ShortList<T> instead of ShortList.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Devin Henderson
Greenhorn

Joined: Dec 01, 2009
Posts: 16
I did it! Figured it out!

Here is my ShortList class:



Here is my ShortUnorderedList class:


Thanks again for the help guys! Really appreciate it!
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Devin:

Just one minor nitpick: I would change the name of your concrete class to UnorderedShortList. One usual naming convention is ExtendingExtended or ImplementingImplemented, where Extended is the name of the abstract class (e.g., ShortList), and Extending is the concrete implementation, which in this case is Unordered.

John.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making Class Generic