• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is the use of interfaces in Java

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just look at Head First Java
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 63
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
please buy my thing and then I'll have more money:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic