• 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

Interfaces

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can interfaces implement another interface or extend another class?
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic