• 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

Implementation of java.util.List abstract method get()

 
Ranch Hand
Posts: 48
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

According to the documentation, method get() in java.util.List is an abstract method. If it's abstract, how come that method execute() below can print "dogs". I thought abstract methods have no implementation?



Thank you for your help in advance.
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're missing a parenthesis on line 3.

I had to trace this through my IDE to get your answer and it was a merry chase.

The "of" method of List doesn't actually return a List, since List is an interface, not a class. But it does return an ImmutableCollection, and ImmutableCollection implements List, so get() is defined as a guaranteed member method for an ImmutableList. So digging around in ImmutableLiist I find ­— guess what? A get() concrete method implementation. So the abstract get() is realized by the concrete method of the object returned by the "of()" function.

And incidentally, the compiler whines. Because technically, it's not merely a List, it's a List<String>, as determined by the argument types if the of() method.
 
Rod Taylor
Ranch Hand
Posts: 48
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:You're missing a parenthesis on line 3.


I sure was! Thank you for spotting this!

Tim Holloway wrote:
But it does return an ImmutableCollection


Where can I find ImmutableCollection class in Java 17 API documentation? It does not appear in search box..

Thank you
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rod Taylor wrote:Where can I find ImmutableCollection class in Java 17 API documentation? It does not appear in search box..
Thank you



That's because it's a purely internal class, not intended for direct use by application developers. ImmutableCollection is actually a base class for different types of immutable collections, such as ImmutableList.

The point is that you'd treat an ImmutableList<String> exactly like you do a List<String>, but since it's immutable, attempts to alter it would throw an Exception. Just a protective mechanism to avoid the old "constants - aren't/variables - won't" problem.
 
Rod Taylor
Ranch Hand
Posts: 48
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you! I get it now
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic