Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

List interface and toString( ) method

Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The List interface does not specify a toString( ) method, and neither does the Collection interface (the only other interface that List implements). Yet I am able to call this method on an object declared as a List. For example:
List myList;
... (assigns some object to myList)
String s = myList.toString();
Of course I know that Object implements toString, and all objects extend Object, so any object must have a toString method, but since the Interface List doesn't extend Object, I thought my class wouldn't compile. It looks like the compiler assumes that all objects have the methods of the Object class?
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you don't explicitly extend an existing class, your class will then just extend Object, thus getting all the methods from the Object class, including toString(). It also seems that interfaces automatically inherit the methods from Object as well...
Posts: 67425
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you can never instantiate an interface, once you get to the point you have a referecne to an implementing instance, it must be an object, not an interface, and thus will always have the Object methods handy.
That's a very big dog. I think I want to go home now and hug this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic