• Post Reply Bookmark Topic Watch Topic
  • New Topic

confused over implements vs extends  RSS feed

 
Marius Semeon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know implements is for interfaces and extends is for classes, but as far as I was aware the only time you could say extends on an interface was with generics, where you might want to use classes which "extend" classes or interfaces.  However, i'm reading Java The Complete Reference, 9th edition at the moment and this is the second time I've come across the author using implements on a class (can't find other example now as I just assumed it was a mistake). In this case specifically: "FilterInputStream implements InputStream"... But even abstract classes like InputStream I thought were extended.
 
salvin francis
Bartender
Posts: 1649
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marius,

It's quite simple actually,


 
salvin francis
Bartender
Posts: 1649
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding enums for completeness:
 
Marius Semeon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks salvin francis, so I take it the book must be wrong then. Since your examples are:
class EXTENDS class
interface EXTENDS interface
class implements interface

and the book says: class IMPLEMENTS class (i.e FilterInputStream implements InputStream (an abstract class) )
 
Tim Moores
Saloon Keeper
Posts: 4021
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to get you thinking along another dimension: in Java's early days, extending classes was all the rage. These days, implementing interfaces is much more popular. What do you think about that?
 
Marius Semeon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:And to get you thinking along another dimension: in Java's early days, extending classes was all the rage. These days, implementing interfaces is much more popular. What do you think about that?


Hmm, pretty new to coding so wasn't really aware of that (haven't looked much at larch programs, mostly book examples). I guess perhaps because interfaces are more flexible, or because it's convenient to make anonymous interfaces within other classes? I don't know, please do tell
 
salvin francis
Bartender
Posts: 1649
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need a large program to recognize the benefits.
As per the code above, you can write code to use the interface List without worrying about the concrete implementation.
Interface are like a contract. You know for sure that the implementing class will contain the methods. The actual implementation is an abstraction.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!