This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Interface Based Programming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Interface Based Programming" Watch "Interface Based Programming" New topic
Author

Interface Based Programming

vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
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

Joined: Jul 14, 2010
Posts: 754

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.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
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

Joined: Dec 21, 2011
Posts: 13

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

Joined: Oct 13, 2010
Posts: 60
Thanks for the info
Mr sujeet khandelwal
Greenhorn

Joined: Dec 21, 2011
Posts: 13
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

Joined: Oct 13, 2010
Posts: 60
Thanks for your info
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Interface Based Programming
 
Similar Threads
Programming to interface
Difference bet. Process based Multithreading and Thread based Multithreading?
To Eric can J2ME be used in Telecom products?
interface
GUI: Contents Tree