• Post Reply Bookmark Topic Watch Topic
  • New Topic

An interface does not have to be implemented?  RSS feed

 
v thai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
This is weird,


Java would not complain even the CrazyBall class does not implement the methods defined in Bouncable interface.

I thought that whenever that we say a class implements an interface then we always have to implement the concrete method of that interface.

I know that Ball has done the deeds, but then why does Java allow that syntax for such a class like CrazyBall? what design purpose does it served? override a implemented method? that can be done simply extending the Ball class without implementing the Bouncable interface....
 
Tim Moores
Saloon Keeper
Posts: 4032
94
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This way any developer looking at the source code of class CrazyBall, or its javadocs, will know that it implements Ball - which otherwise might not be obvious.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

CrazyBall also extends Ball, so it inherits the implementation of Bouncable from Ball.
There's no reason why it should be required to have CrazyBall override the methods that it inherits from Ball.
 
v thai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jesper,

I understand that there is no reason that we require CrazyBall to override the methods that it inherits, it should only be overrided if the programmer thinks they should, I am just confused that why Java allow the declaration of a interface twice... it is like

and then

which somehow means

implicitly, am I right?
so now if we have


Tim's suggestion that it is used to make code more coherence, from a designer points of view, this could be right... I am thinking is there any other reason, beside that
 
Tim Moores
Saloon Keeper
Posts: 4032
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're over-thinking this. A class implements an interface, or it doesn't. If it does, it may well inherit the methods that do so from a superclass. That's all.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
v thaiI am just confused that why Java allow the declaration of a interface twice[/quote wrote:

As Tim already stated: For documentation purposes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!