aspose file tools*
The moose likes Beginning Java and the fly likes what is the use of interfaces in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is the use of interfaces in Java" Watch "what is the use of interfaces in Java" New topic
Author

what is the use of interfaces in Java

Loganathan Karunakaran
Ranch Hand

Joined: Nov 04, 2008
Posts: 78
Hi,

Could some one please tell me the real use of interfaces. I have searched in Internet , but i didn't get a meaning ful answer. Could some one please give a practical example and explain the use of interfaces in Java.

Thanks
Loganathan


OCPJP 1.6
Raj chiru
Ranch Hand

Joined: Aug 12, 2008
Posts: 141
You just look at Head First Java
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
Please use the "Search" link at the top of the page to find the many, many previous discussions on this topic.


Ping & DNS - my free Android networking tools app
Moguluri Ravi Kiran
Ranch Hand

Joined: Apr 16, 2010
Posts: 62

1) interface in User point of view.

interface is just like a button provided at front side of the TV set .it is a communication medium between you and the internal implementation of the button inside the TV for which it is intended.

you just need to know the feature of the interface(such as Contrast , brightness , volume buttons) and need to search for the class that has already implemented it in the API/somewhere else . And Just make use of that class in your program.

2) interface in developer point of view.

i will explain this with the following...

suppose that there are two developers satdown to develope some code to calculate the intrest of the balence of account.

Developer 1:

Just starts a class and he needed to know the balence of a perticular account. rather than going to create another method to get the balence, he just creates an interface called(say) as Intrest calculting Class with "getBal()' decreases the coheciveness of the class.


interface iBalanceEnquiry
{
public float getBalance(String accountID);
}

this will be given to the 2 nd developer and Developer 1 says , see boss you need to develope a class that implements the iBalanceEnquiry.

Developer 2:

he understands the problem of the Developer1 and creates "class BalancePickup implements iBalenceEnquiry". in this he overriders method in the interface which takes accountID as parameter ,picksup the bal. availabel from database & returns inside the Developer 1's class where it is required...


Thus an interface is a contract between classes.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

Think of an interface as being like a USB port. It doesn't matter what is on the other end, as long as it conforms to that interface. An iPod, a Zune, a cheap $10 music player...any thing will work, as long as it conforms to that standard.

interfaces in java do the same thing. An interface defined how an object will communicate with the rest of the world. Once that is established, the specific implementation, and even the object type, can change all they want, and it won't effect anything else.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Moguluri Ravi Kiran
Ranch Hand

Joined: Apr 16, 2010
Posts: 62

fred rosenberger wrote: Once that is established, the specific implementation, and even the object type, can change all they want, and it won't effect anything else.



Really i didnot understand the SIGNIFICANCE of the line quoted. can i get that elaborated.

Thanks.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

this question has been asked and answered many times in this forum. Please try searching for similar topics and you'll find what that for which you're looking.
Loganathan Karunakaran
Ranch Hand

Joined: Nov 04, 2008
Posts: 78
Thanks to everyone for spending your valuable time in answering my question. Also I will search for the similar issues before posting here.
Robby Robinson
Greenhorn

Joined: May 22, 2010
Posts: 6
An interface is a contract wit the class defining what the class provide to earn the right to such type(interface name).
When a 3rd +type you define a interface.

for say a furniture database .
you have bed, tables a chairs. all come in a assortment of materials it would tedious i to define a class for each furniture type(beds, tables etc). so instead you define interface for all the materials .so when define a new piece of furniture you simply has it implement the appropriate materials interface.

Public interface materials }
private material;}
also different behaviors

A car and bicycle both accelerate but with different ways so you want a interface the includes a accelerate method that car and bicycle would implement
 
 
subject: what is the use of interfaces in Java