• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on Interfaces  RSS feed

 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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?
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • 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: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Landry wrote: . . . Interfaces - must be implemented and all methods are abstract. . . .
That will probably change in Java8.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!