Why Interface? What is Interface?

Posted on 2006-11-19
Last Modified: 2010-03-31
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.
Question by:wjh7554
LVL 86

Accepted Solution

CEHJ earned 20 total points
ID: 17974634
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
LVL 13

Assisted Solution

marchent earned 20 total points
ID: 17974662
u can implemet more than one interface, where u can only extend a single class
LVL 24

Assisted Solution

sciuriware earned 20 total points
ID: 17974707
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.

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

LVL 10

Assisted Solution

jaggernat earned 20 total points
ID: 17975397
>>>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.


Assisted Solution

bnblazer earned 20 total points
ID: 17976424
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.


Expert Comment

ID: 17976538

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question