Java Tutorial

Interfaces

  1. Interfaces are basically used to develop user defined data types.
  2. With respect to interfaces we can achieve the concept of multiple inheritances.
  3. With interfaces we can achieve the concept of polymorphism, dynamic binding and hence we can improve the performance of a JAVA program in turns of memory space and execution time.

An interface is a construct which contains the collection of purely undefined methods or an interface is a collection of purely abstract methods.

Syntax for defining an interface:

Interface <interface name>
{
    Variable declaration;
    Method declaration;
}

Interface is a keyword which is used for developing user defined data types. Interface name represent a JAVA valid variable name and it is treated as name of the interface. With respect to interface we cannot create an object directly but we can create indirectly.

Variable declaration represents the type of data members which we use a part of interface. Whatever the variables we use in the interface are meant for general purpose (the variables like PI, e, etc.).

Whatever the variables we write in the interface, they are by default belongs to:

public static final xxx data members

xxx represents data type, variable name and variable value.

All variables must be initialized (otherwise it will be compilation error). Method declaration represents the type of methods we use as a part of interface. All the methods of interface are undefined methods and to make those methods as abstract, the JAVA programmer need not to write a keyword abstract explicitly before the declaration of interface method.

Since all the methods of interface are meant for general purpose hence they must give universal access. To make it as universal access the JAVA programmer need not to write a keyword public explicitly before the method declaration. Hence, by default all the methods of interfaces belong to public abstract methods.

For example:

Interface i1
{
    Int a; //invalid since, initializing is mandatory 
    Int b=20;
    Void f1 ();
    Void f2 ();
}
   

Note:

Whenever we compile an interface, we get <interface name>.class as an intermediate file, if no errors are present in interface.

Syntax-1 for reusing the features of interface(s) to class:

[abstract] class <clsname> implements <intf 1>,<intf 2>.........<intf n>
{
    variable declaration;
    method definition or declaration;
};

In the above syntax clsname represents name of the class which is inheriting the features from 'n' number of interfaces. 'Implements' is a keyword which is used to inherit the features of interface(s) to a derived class.

Note:

When we inherit 'n' number of abstract methods from 'n' number of interfaces to the derived class, if the derived class provides definition for all 'n' number of abstract methods then the derived class is known as concrete class. If the derived class is not providing definition for at least one abstract class and it must be made as abstract by using a keyword abstract.

  1. One class can extend only one class.
  2. One class can implement 'n' number of interfaces.
  3. One interface can extends more than one interface.
  4. Interface cannot implements or extends a class. Since defined things cannot be made as undefined things.

Syntax-2 inheriting 'n'number of interfaces to another interface:

interface <intf 0 name> extends <intf 1>,<intf 2>.........<intf n>
{
    variable declaration cum initialization;
    method declaration;
};
                        

For example:

interface I1
{
    int a=10;
    void f1 ();
};

interface I2 extends I1
{
    int b=20;
    void f2 ();
};
                        

If one interface is taking the features of another interface then that inheritance is known as interface inheritance

Syntax-3:

[abstract] class <derived class name> extends <base class name> implements <intf 1>,<intf 2>.........<intf n>
{
    variable declaration;
    method definition or declaration;
};
                        

Whenever we use both extends and implements keywords as a part of JAVA program we must always write extends keyword first and latter we must use implements keyword.

Important points:

  1. An object of interface cannot be created directly since it contains 'n' number of abstract methods. An object of interface can be created indirectly. An object of interface = an object of that class which implements that interface.
  2. An object of base interface contains the details about those methods which are declared in that interface only but it does not contain details about those methods which are specially available in either in derived classes or in derived interfaces.
  3. Interfaces should not be final.
  4. An interface does not contain Constructors.