Why Interface? What is Interface?

I have read enough trying to understand what is Java's Interface, why we should use it and also what is the main reason to use it.

I understand, in technical, once i have "implements" one interface class, i have to put the method within that interface class into my program.

Why i should do that? I dont know. Because i that method i still can put it in if i didnt implemnts that Interface.

Cany someone share with me some practical usage of Interface in our real time system development?

thank you.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

CEHJConnect With a Mentor Commented:
An interface is largely for the benefit of clients of that interface. It defines *what* a client can expect from an implementer and leaves the *how* to the implementer
marchentConnect With a Mentor Commented:
u can implemet more than one interface, where u can only extend a single class
sciuriwareConnect With a Mentor Commented:
You can also 'see' an Interface as a qualification that a class can have.
This qualification may demand some methods to be added.
Other parts of the program may now treat your class as 'seen' by the interface only.

An example:    When you say that your class     "implements ActionListener"
then a button will accept an object of that class as is
and will blindly call the method "actionPerformed()" when the button is pressed.

This is a way to make any object able to handle button clicks.

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

jaggernatConnect With a Mentor Commented:
>>>Cany someone share with me some practical usage of Interface in our real time system development?

say you have a client trying to access some methods. You can expose an interface to the client . the interface  will have the methods which the client can use but the actual implimentations of those methods can be defined in an implimentation class. That implimentation class would impliment the interface and contain the method body. This way the actual  implimentation class remains safe and the client only sees and interacts with the interface.

bnblazerConnect With a Mentor Commented:
Think of an interface like a car radio.  One can assume that all car radios can tuneStation(), setVolume(), and powerOnOff().  It is just that all car radios do it differently.

In the case of the interface, it is set up so that all car radios do these things, and leaves it up to the individual radio to do it they way they would like to do it.  It is a contract of sorts.  If you implement the interface you are making a contract that all of these CarRadio methods will be used.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.