• 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

Question on Interfaces

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From my reading, Interfaces and Abstract classes appear straightforward.

Interfaces - must be implemented and all methods are abstract.

Sounds easy enough.

Going through the Java Docs for List (http://docs.oracle.com/javase/7/docs/api/java/util/List.html) and it shows List is an interface which extends Collection which is yet another interface.

I thought interfaces are implemented and not extended.
I thought all methods in an interface are abstract, yet most if not all of the List class are not abstract.

Are the Java specific classes exceptions to the rule?
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are not exceptions.
Any concrete (non-abstract) class implementing an interface must implement all of its methods.
Any concrete (non-abstract) class extending an abstract class must override all of its abstract methods.
A class may implement an interface any number of interfaces as long as there is no conflict between methods.
A class can't extend an interface.
A class may extend only one other class.
An interface may extend any number of interfaces as long as there is no conflict between methods.
An interface can't implement any interface.
An interface can't implement nor extend a class.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Landry wrote:I thought interfaces are implemented and not extended.


A class implements an interface. An interface can extend another interface, meaning any class who implements the first would need to implement the methods of both interfaces. So an ArrayList, which implements List, must also implement all the methods in Collection, and for inheritance and assignment purposes the ArrayList IS-A List and also IS-A Collection.

I thought all methods in an interface are abstract, yet most if not all of the List class are not abstract.


All interface methods are implicitly public and abstract, they don't need to be declared as such, just having the method definition in the interface makes it so. So yeah, you are right, the methods are all abstract but the List interface doesn't actually have to use the word, since it is understood from the interface definition.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Landry wrote:I thought interfaces are implemented and not extended.


An interface can extend another interface.

Tom Landry wrote:I thought all methods in an interface are abstract, yet most if not all of the List class are not abstract.


They are but they don't need to be explicitly marked as such. The compiler automatically treats all interface methods as abstract even if the source code doesn't mark them as such.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote: . . . A class may implement an interface. . . .

A class may implement several interfaces as long as there are no conflicts between methods.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Pawel Pawlowicz wrote: . . . A class may implement an interface. . . .

A class may implement several interfaces as long as there are no conflicts between methods.


Right!
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Landry wrote: . . . Interfaces - must be implemented and all methods are abstract. . . .

That will probably change in Java8.
 
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic