What are interfaces in java

 


                            Interfaces


Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body. In practice, this means that you can define interfaces which don’t make assumptions about how they are implemented. Once it is defined, any number of classes can implement an interface. Also, one class can implement any number of interfaces. To implement an interface, a class must create the complete set of methods defined by the interface. However, each class is free to determine the details of its own implementation. An interface is typically used when disparate (i.e., unrelated) classes need to share common methods and constants. This allows objects of unrelated classes to be processed polymorphically objects of classes that implement the same interface can respond to the same method calls. Programmers can create an interface that describes the desired functionality, then implement this interface in any classes that require that functionality.



Defining an Interface


Syntax:

accessModifier interface nameofInterface

{

return-type method-name1(parameter-list);

return-type method-name2(parameter-list);

type final-varname1 = value;

type final-varname2 = value;

// ...

return-type method-nameN(parameter-list);

type final-varnameN = value;

}

Here, access is either public or not used. When no access specifier is included, then default access results, and the interface is only available to other members of the package in which it is declared. When it is declared as public, the interface can be used by any other code. name is the name of the interface, and can be any valid identifier.

Notice that the methods which are declared have no bodies. They end with semicolon after the parameter list. They are, essentially, abstract methods; there can be no default implementation of any method specified within an interface. Each class that includes an interface must implement all of the methods.

Variables can be declared inside of interface declarations. They are implicitly final and static, meaning they cannot be changed by the implementing class. They must also be initialized with a constant value. All methods and variables are implicitly public if the interface, itself, is declared as public.

e.g.

interface Callback

{

void callback(int param);

}




Implementing Interfaces


To implement an interface, include the implements clause in a class definition, and then create the methods defined by the interface.

Syntax:

access class classname [extends superclass]

[implements interface [,interface...]]

{

// class-body

}

access is either public or not used. If a class implements more than one interface, the interfaces are separated with a comma. If a class implements two interfaces that declare the same method, then the same method will be used by clients of either interface. The methods that implement an interface must be declared public. Also, the type signature of the implementing method must match exactly the type signature specified in the interface definition. It is both permissible and common for classes that implement interfaces to define additional members of their own.

Example:


interface myInterface

{

void test(String msg); //abstract method

}

class interfaceImplementClass implements myInterface

{

public void test(String msg)

{

System.out.println(msg);

}

}

class InterfaceTest

{

public static void main(String args[])

{

interfaceImplementClass testObj = new interfaceImplementClass();

testObj.test("Hello M");

}

}
Accessing Implementations Through Interface References

You can declare variables as object references that use an interface rather than a class type. Any instance of any class that implements the declared interface can be referred to by such a variable. When you call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to. This is one of the key features of interfaces. The method to be executed is looked up dynamically at run time, allowing classes to be created later than the code which calls methods on them. The calling code can dispatch through an interface without having to know anything about the ―callee.‖

Example:
interface myInterface
{ void test(); }
class J implements myInterface
{
public void test()
{ System.out.println("Hello M! This is H"); }
}
class S implements myInterface
{
public void test()
{ System.out.println("Hello M! This is N"); }
}
class interfaceTest
{
public static void main(String args[])
{
myInterface interfaceVariable;
H object_of_H = new H();
interfaceVariable = object_of_H;
interfaceVariable.test();
interfaceVariable = new N();
interfaceVariable.test();
}
}

Partial Implementations

If a class includes an interface but does not fully implement the methods defined by that interface, then that class must be declared as abstract.

Example:
abstract class Incomplete implements myInterface
{
void show()
{ System.out.println(“Hello Mehdi”); }
}

Variables in interfaces

You can use interfaces to import shared constants into multiple classes by simply declaring an interface that contains variables which are initialized to the desired values. When you include that interface in a class (that is, when you ―implement‖ the interface), all of those variable names will be in scope as constants. This is similar to using a header file in C/C++ to create a large number of #defined constants or const declarations. If an interface contains no methods, then any class that includes such an interface doesn’t actually implement anything. It is as if that class were importing the constant variables into the class name space as final variables.

Example

interface sharedConstants
{ final int VERYLOW = 1;
final int LOW = 2;
final int NORMAL = 3;
final int HIGH = 4;
final int VERYHIGH = 5;
}
class M implements sharedConstants
{
void test()
{
System.out.println("My priority is " + NORMAL);
}
}
class J implements sharedConstants
{
void test()
{
System.out.println("My priority is " + VERYHIGH);
}
}
class testConst
{
public static void main(String args[])
{
M objofM = new M();
objofM.test();
N objofN = new N();
objofJ.test();
}
}

Interfaces can be shared


One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain.

Example

interface H
{
void testH();
}
interface M extends N
{
void testM();
}
class S implements N
{
public void testH()
{
System.out.println("This is H");
}
public void testM()
{
System.out.println("This is M");
}
public static void main(String args[])
{
S obj = new S();
obj.testM();
obj.testH();
}
}

Post a Comment

Please do not enter any spam link in the comment box