• 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

Programming to an interface

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Recommended practice is to ensure that we code objects to an interface,
enabling us to exploit polymorphic behaviour. Does this mean that
all classes I create should implement an interface.
regards,
Mo
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

in short: no, it doesn't mean that.

A little longer: First, polymorphic behavior does not depend on a class having an interface; a class hierarchy without any interface can exhibit polymorphic behavior.

Providing an interface allows you to separate the implementation of an abstraction (i.e. the class) from its interface (i.e. the interface itself). You can exchange or modify the implementation without affecting code programmed against your interface.

You would typically only provide an interface for a class when the benefits outweigh the cost. It is good practice to provide (Java) interfaces for the public/published interfaces of components or subsystems.

Cheers,
Oliver
 
mo sayed
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A little longer: First, polymorphic behavior does not depend on a class having an interface; a class hierarchy without any interface can exhibit polymorphic behavior.

Providing an interface allows you to separate the implementation of an abstraction (i.e. the class) from its interface (i.e. the interface itself). You can exchange or modify the implementation without affecting code programmed against your interface.

You would typically only provide an interface for a class when the benefits outweigh the cost. It is good practice to provide (Java) interfaces for the public/published interfaces of components or subsystems.



Sorry I should have been clearer. I did not convey the full benefits of programming to an interface. You captured this beautifully. My intent was to
determine to what extent this practice should be executed. As with most things it seems that this is a case of fine judgement. Thanks for the answer.
regards,
Mo
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more important thing. Whenever you encounter this term "Programming to an interface". Don't ever just take it as Interface in Java. It can be an abstract class or sometimes a normal superclass.

Cheers.
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic