Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces

 
Claudio Roitman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I was looking at Java System classes sources and I got some dudes about Interfaces:
Look at the following class declarations:
1)public class LinkedList extends AbstractSequentialList
implements List, Cloneable, java.io.Serializable {...}
2)public abstract class AbstractSequentialList extends AbstractList {...}
and
3)public abstract class AbstractList extends AbstractCollection implements List {}
My question is: Why is it necessary to put "implements List" in LinkedList declaration?
LinkedList is a subclass of AbstractList
( AbstractList --> AbstractSequentialList ---> LinkedList ) AbstractList already declares it implements List interface, so I think LinkedList does not need to declare it explicitly. Am I right?.
BTW , I am taking the SCJP Test in 3 days...I am very nervous!!!
Thanks and sorry about my English!
My question is: Why is it necesary to put "implements List" in LinkedList declaration?
LinkedList is a subclass of AbstractList
( AbstractList --> AbstractSequentialList ---> LinkedList ) AbstractList already declares it implements List interface, so I think LinkedList does not need to declare it explicitly. Am I right?.
BTW , I am taking the SCJP Test in 3 days...I am very nervious!!!
Thanks and sorry about my english!
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy -- you're right... if class A extends class B, then class A automatically IS-A anything that B implements. So A does *not* need to explicitly declare that it implements the things that it IS as a result of being a subclass of B. In other words, yes, if your superclass implements an interface, then you also implement that interface, whether you explicitly say it or not.
So you do not HAVE to say it, but it's a good practice. It makes it explicit in your API that you implement something without the user having to search the inheritance tree to figure out everything that your class implements. Especially good when your class implements things not provided by the inheritance structure, so that when you DO show the interfaces you implement, you list them all, even if some are really coming from other superclasses.
cheers,
Kathy
 
Claudio Roitman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kathy!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic