File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface Based Programming

 
vShyam Sundar
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just started to learn EJB and i m very much confused with the term Interface Based Programming?

Can any one help me in understanding what an interface based programming is?

How can code re usability and loose coupling is achieved through interface based programming?


Thanks in Advance,
Shyam.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, vShyam. Welcome to JavaRanch.

If you program to interfaces you will be able to refactor you code without the need of change your client code.

Image that a client has the interface:

Your class could change the implementation that your client would neve notice:

In this post you can find about the Strategy pattern that works with interfaces: Design Pattern – Strategy.
 
vShyam Sundar
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks helbert,

So client can invoke the method printName();Through the interface. But how code re usability can be achieved?

I can even call the printName() using an instance of class and change the method implementation.

I'm confused with the below question

What was the advantage of invoking the method through interface instance rather than class instance?

 
Mr sujeet khandelwal
Greenhorn
Posts: 13
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This may help you..

In interface based programming clients communicate with objects exclusively through interface references. This ensures that clients never build dependencies on the classes that serve up objects. This allows programmers to revise their object code without worrying about breaking client code. Clients can get run-time type information from objects. A client can always query an object and ask if it supports a specific interface. If the requested interface is not supported, the client can discover this and degrade gracefully. This lets programmers revise components and applications independently.
 
vShyam Sundar
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info
 
Mr sujeet khandelwal
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your question "What was the advantage of invoking the method through interface instance rather than class instance?"

See there are 3 things... Business Interface, Business class which implements Business Interface and Client code.

- In interface based programming... advantage is Client code is not changed whenever we change code of business class (business logic)...
Once interface is created it is just used for implementation and it is never changed.
Client uses only interface to access EJB's method so client code is also never changed as interface is never changed.
Now class which implements Business interface, business logic can be changed in this class and that will not impact client code as we are can not change method definition.

- Now consider the case where only 2 things we have Business class and client code...
Client uses business class to access methods.
As Business class developer is not aware of client code and while doing changes in business logic if he makes any changes in business method's definition that will surely harm the client code.

Thanks.
 
vShyam Sundar
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your info
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic