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

what is the use of interfaces in Java

 
Loganathan Karunakaran
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Raj chiru
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just look at Head First Java
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the "Search" link at the top of the page to find the many, many previous discussions on this topic.
 
Moguluri Ravi Kiran
Ranch Hand
Posts: 63
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Moguluri Ravi Kiran
Ranch Hand
Posts: 63
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic