Win a copy of Svelte and Sapper in Action this week in the 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • 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
 
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic