Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use interface in an application ?

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone please explain/elaborate the following two sentences on when to use interface :

1. If you need to change your design frequently,
you should prefer using interface to abstract.

2. Coding to an interface reduces coupling and interface
inheritance can achieve code reuse with the help of object composition.
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use an interface when there is a need to have a central system or all other parts of your application is having something common between them then it's a good call to create an interface.

kaustubh
 
Gokul Nanthakumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you want to create a loosely coupled design Interface is the best pet.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search here on the ranch; there are other threads on similar subjects (for example in Beginning Java) which may be helpful.
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:Could someone please explain/elaborate the following two sentences on when to use interface :

1. If you need to change your design frequently,
you should prefer using interface to abstract.

2. Coding to an interface reduces coupling and interface
inheritance can achieve code reuse with the help of object composition.


I think interfaces and abstract classes are good for both of those situations. The advantage an interface has is that your class can implement multiple interfaces and your class can have a parent class. With abstract classes, you are forced to extend the abstract class, preventing you from extending any other classes.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I earlier wrote:Please search here on the ranch; there are other threads on similar subjects (for example in Beginning Java) which may be helpful.
Try this thread too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic