• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making Class Generic

 
Devin Henderson
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devin:

Can you post the code for your ShortList class?

John.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Devin Henderson
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic