File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Requirement of Interface in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic

Requirement of Interface in Java

java tech

Joined: Jul 13, 2005
Posts: 4
Interface can fully abstract a class interface from its implementation that�s using this we can specify what a class do but not how it does it. Interfaces are similar to classes but they don�t have instance variables and there methods are declared without a body. Once it is defined, any number of classes can implement interface and one class can implement any number of interfaces. Each class is free to determine the details of its own implementation.
For example:

Interface callback{
Void callback(int param)

Access is either public or not specified, if not specified then it is considered to be default and interface is available to other members of the package. Variables can be declared inside interface declaration. They are however implicitly final and static that is they cannot be changed by the implementing class. Variable and method are public if access declaration is public.

Implementing Interfaces

Class client implements callback{
Public void callback(int p){
System.out.println(�callback called with � +p);

If a class implement two interface that declare the same methode then the same method will be used by clients of either interface.
A class which implements interface can be define additional members of its own class.

Accessing Implementations through interface References

We can also declare variable as object references that use an interface. Any instance of a class that implement the declared interface can be referred to by such a variable.When we call a method through one of the references, the correct version will be called based on the actual instance of the interface being referred to. The methode to be executed is looked up dynamically at run time ,allowing classes to be created later than the code which calls method on them.

For Examle:
Class testIface
Public static void main(String args[]){
Callback c= new client();
Variable c is declared to be of the interface type callback, but it is assigned as instance of client. C can be used to access the callback() method ,it cannot access any other member of client class .An interface variable only has knowledge of the methods declared by its interface declaration, this c could not be used to access non interface method since it is defined by client but not callback.
If a class includes an interface but doesn�t fully implement the methods defined by that interface, then that class must be declared as abstract.

Abstract class Incomplete implements callback{
Int a,b;
Void show(){
System.out.println(a+ � �+b);

Interface tool is an important part of java program environment. In C++ we have multiple inheritance so a class can inherit from many classes but since it causes a lot of ambiguity this procedure is avoided in java and instead we use interfaces which once declared can be called in any number of classes any number of times.
Steve McCann
Ranch Hand

Joined: Oct 20, 2004
Posts: 81
If this is a question, I've missed it...
Matt Fielder
Ranch Hand

Joined: Oct 27, 2004
Posts: 158
see this post
I agree. Here's the link:
subject: Requirement of Interface in Java
It's not a secret anymore!