aspose file tools*
The moose likes Beginning Java and the fly likes Subclassing a generic class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Subclassing a generic class" Watch "Subclassing a generic class" New topic
Author

Subclassing a generic class

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I want to subclass ArrayList class with an implementation that only uses String elements, and override the contains method to do a case-insensitive test for equality. I know there are simpler ways to get this functionality but I was really just trying it out as an experiment. Here is the code so far:

That yields the following compiler error:
E:\Java Stuff\Misc. Test Classes\CustomStringList.java:15: cannot find symbol
symbol : method equalsIgnoreCase(String)
location: class java.lang.Object
if (element.equalsIgnoreCase(str))
^
Note: E:\Java Stuff\Misc. Test Classes\CustomStringList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

Tool completed with exit code 1

So I tried a cast like this

That yielded an identical compiler error:
E:\Java Stuff\Misc. Test Classes\CustomStringList.java:16: cannot find symbol
symbol : method equalsIgnoreCase(String)
location: class java.lang.Object
if (element.equalsIgnoreCase(str))
^
Note: E:\Java Stuff\Misc. Test Classes\CustomStringList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

Tool completed with exit code 1

So my question is, is there anyway this can be done?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Never mind, I figured it out. I just had to remove the generic type definition in my class name.

Incidentally, is there a workaround to using the instanceof keyword here?
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

Incidentally, is there a workaround to using the instanceof keyword here?

Yes.
First, you must abandon the orthodox methods of reasoning - they are horribly contrived. I realise this is the beginner's forum, and nobody likes anything slightly "left wing" in here, so you can contact me offline for further clarification if you choose.

An incomplete set of axioms of "software in Java":
- You can (and should) *always* avoid the use of instanceof
- You can (and should) *always* avoid a cast
In short, you can achieve these objectives with appropriate abstractions (insert lengthy explanation here).

However, you have restricted your context to "software in Java using ArrayList". A common mistake here is to assume legitimacy of the abstraction that you are using - ArrayList (and many collection types) are flawed (they contain implicit contradictions in their objectives through formal analysis). The subjectivity of this assertion often digresses to identifying language flaws that force inappropriate abstraction *no matter what* - only that for many types, the "inappropritateness" is taken to an extreme.

Do not assume the legitimacy of ArrayList and analyse it. While there are infinite flaws ("infinite" meaning literally - not to exaggerate a real number), the particular flaw that you are observing is really quite trivial - and of course, you will be told by the marketing/JSR folk that the "contains" method of the List type must accept type Object for the remainder of that type's life for reverse compatibility. I might digress to the underlying flaw of this reasoning, but again, I am probably already exceeding the permitted dialogue in a beginner forum.

Instead, I will simply respond, "does List/ArrayList meet your requirement?" - Clearly not, since you observe a failure of abstraction (and you even acknowledge it with "how can I not use instanceof?"). The answer then is "either concede to the flawed type, or write the type yourself" - these are your only two options within the context of Java. Once you restrict yourself to ArrayList/List, you lose the latter option, in which case, the answer simply becomes "no, you cannot avoid instanceof".

As a side note, why are you extending ArrayList? Many code style checkers will bonk you on the head for that (despite only minimising the damage of the undocumented flaws that I refer to - instead of eliminating them).

I hope this helps.


Tony Morris
Java Q&A (FAQ, Trivia)
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
As a side note, why are you extending ArrayList? Many code style checkers will bonk you on the head for that (despite only minimising the damage of the undocumented flaws that I refer to - instead of eliminating them).


Basically I was doing it just to see how it could be done. It was a quick and dirty solution to a contrived problem. However, I would like further elaboration on how this is bad style. Is it just because extending a library class in general is frowned upon. Or is it that (as someone else pointed out to me) with this dirty solution I am breaking the contract of the Collections.contains() method.

From your comments, and a quick look at the CantractualJ website, I gather that in your opinion the best solution would be to define an interface that suits my requirements and then develop a concrete implementation of that interface.
, and nobody likes anything slightly "left wing" in here,
On the contrary, I get a lot out of your comments and enjoy reading opinions from all sides of "the great Java divide".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Subclassing a generic class