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.
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.
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.
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.
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...
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
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 }
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