Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Interfaces

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can interfaces implement another interface or extend another class?
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No interfaces can only extend other interfaces

interface1 extends interface2

this is the only possible thing to do with interfaces.

however abstract classes, can implement interfaces and they can either provide a definition for the interface methods or leave it for the first concrete class that extends that abstract class.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

omar al kababji wrote:No interfaces can only extend other interfaces

interface1 extends interface2

this is the only possible thing to do with interfaces.

however abstract classes, can implement interfaces and they can either provide a definition for the interface methods or leave it for the first concrete class that extends that abstract class.



Yes with regards to inheritance this is true. However it is not only Abstract Classes which can implement interfaces. Concrete classes themselves are also able to implement interfaces, and in doing so are required to fulfill the contract of the methods declared in the interface.

Additionally interfaces are a fine example of polymorphism if the technique of "programming to an interface" is used. In Java it is strongly recommended that any arguments to a parameter for a method should be as general as possible. This ensures among other things, that there is a good degree of flexibility in your code. What it means in code, is that any object maybe passed to your method providing it implements the required interface, so in the future if you have additional Objects with a varying degree of behaviors and attributes they may still be handled by your Method providing they implement the interface.

For example take the following example (tried and tested in Eclipse);




This should produce the following output:



Hope this of use
 
    Bookmark Topic Watch Topic
  • New Topic