• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Subclassing a generic class

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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".
 
Not so fast naughty spawn! I want you to know about
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic