Interface: – A java is programming language like C++ but it does not support multiple inheritance. The designer of java could not overlook the importance of multiple inheritance. A large number of real life application they require use of multiple inheritance whereby we inherit methods and property from several distinct classes. Since C++ like implementation of multiple inheritance proves difficult and adds complexity to the language, java provide an alternate approach known as interface to support the concept of multiple inheritance. Although a java class cannot be a subclass of more than one supper class, it can implement more than one interface, thereby enabling us to create classes that build upon other classes without the problems created multiple inheritance.

An interface in basically a kind of class. Like classes, interface contained method and variables but with a major difference. The difference is that interfaces define only abstract method and final field. This mean that interfaces do not specifies any code to implement these method and data field contains only constant. There for, it is responsibility of the class that implements an interface to define the code for implementation of these methods. Syntax of the interface is –

interface <Interface Name>
{
                Variable Declaration;
                Method Declaration;
}

Here, interface is a keyword and interface name is a valid identifier. Variables inside interface can be declared in the following format –

static final <Data Type> <Variable Name=Value>;

All the variables are declared as constant. Methods are declaring inside the interface without its buddy. A method can be declared in the following format –

<Return Type> <Method Name>(Parameter list if any);

Here semi clone terminator must be used after declaration of method. An example of an interface declaration is –

interface item
{
                Static final int code=1001;
                Static final String name=”Fan”;
                void display();
}

Existing interfaces: – Like classes, interfaces can also be extended. Only existing class or interface can be extended. An interface can be sub interfaced from other interfaces. The new sub interface will inherit all the members of the supper interface in the manner similar to subclasses. This is achieved by using keyword extend. Syntax of interface with extends clause is –

interface <Name2> extends <Name1>
{
                Body of Name2
}

Implementing interfaces: – A interface is used as super classes whose properties are inherited by classes. It is necessary to create a class that inherits the given interface. Syntax of a class with implements clause.

class <Class Name> implements <Interface Name>
{
                Body of class name
}

The implement clause can be used for multiple interfaces. If more than one interfaces inherit with a class, all the interfaced are implemented by the implements clause. Syntax of the multiple inheritance with number of interfaces are –

class <Class Name> extends <Supper Class Name> implements <Interface1,interface2,_ _ _ >
{
                Body of class name
}

 

Advertisements