• 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

Interface Extend vs Implement Another Interface

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What is the difference between an interface extending (extend keyword) one or more interfaces vs implementing (implements keyword) one or more interfaces?
I see that the concreate class that implement the interface (wherever in the inheritance tree) need to anyway implement all the methods from all the interfaces extended or implement.

But is there a reason why I would rather extend and not implement an interface on an interface?
Doesn't make sense to provide both features in Java.

thanks
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difference: one will work, the other will not compile.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:The difference: one will work, the other will not compile.



I think he is talking about InterfaceB extends InterfaceA and ClassA implements InterfaceB or ClassA implements InterfaceA, InterfaceB, which will both compile...

When you make that decision just use common sense. If an interface adds to another interface and will in every case need the functionality the extended interface has, it makes sense to extend an interface.

Imagine an interface called DatabaseConnectionSupport which provides functionality to obtain and release a database connection.
Now you have another interface called DatabaseQuerySupport which gives functionality to perform basic database query needs.

So


makes perfect sense. Note that whenever you ship a DatabaseQuerySupport you also provide the ConnectionSupport.

If you have a Dog and a Cat interface it does not make sense to extend them and put them into one type hierarchy because they are simply two totally different things.
 
Lennie De Villiers
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops i got it wrong, an interface can't implement another interface (not even one) but can only extend another interface or more then one interface.
This is where the confusing come in where an interface vs a class.
reply
    Bookmark Topic Watch Topic
  • New Topic