• 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

An interface does not have to be implemented?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Saloon Keeper
Posts: 7585
176
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
I will suppress my every urge. But not this shameless plug:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic