This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what are the advantages of implementing interface over extending class ???

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody tell me all the advantages of implementing interface over extending class ???
 
Campbell Ritchie
Sheriff
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This goes with your other question.

You can mimic multiple inheritance by implementing several interfaces. The problem with diamond inheritance Jesper Young mentioned doesn't apply if you have two methods with the same signature, if both methods are completely empty.

You can implement several interfaces and only extend one class. An interface provides a set of method headers; any method using your object under the name of its interfaces can rely on those methods being available, particularly if you have implemented them according to the instructions for the interface.

Read the API documentation for Comparable<T> for an example.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic