Search

Interface in Java



Interface in Java

Today we will discuss about Interface in Java. We know that concrete class of java can deals with specific requirement and unable to deal with common requirement. If concrete classes deals with common requirement we get three limitations they are
(a)   Takes more memory space
(b)   Takes more execution time
(c)    Less performance

To avoid these problems of concrete class in dealing with common requirement we use the concept of abstract class.
If we are abstract class for dealing with common requirement such application will get the following advantages

(a)   Less memory space
(b)   Partial less execution time
(c)    More performance

Along with the above advantages, we may come across the following limitation in abstract classes

(a)   Abstract classes never participate in multiple inheritance.
(b)   Abstract classes provide only common reusable features.
(c)    And they are unable to provide universal common reusable feature.

We know that an abstract class is a collection of defined method and undefined methods it is recommended to override undefined / abstract methods of abstract class. But not recommended to override defined method of abstract class that is abstract classes are containing both recommended and un-recommended facilities due to this applications of abstract classes gets partial less execution time. But not completely gating less execution time.


Conclusion
To avoid the above problem of abstract classes we use the concept of interfaces.
 


 



Interface concept is always used for developing universal user defined data type.
To develop interfaces concept to related application, we make use a keyword called interface. Each and every interface name in java acts as universal user defined datatype.

Definition of interface

An interface is a collection of public static final xxx data members and public abstract methods here xxx represents datatype, variable name and variable value.

                                                            OR

An interface is a collection of universal common reusable data member and universal common reusable methods.
Syntax for defining an interface

Interface   <interfacename>
{
Variable declaration cum initialization;
Method declaration;
}

In the above syntax
     
     (1)         Interface is a keyword used for developing universal user defined datatype.
     (2)       <interfacename> represents java variable value name treated as name of the interface. 

      Programmatically every interface name acts as universal user defined datatype.
With respect to an interface we can’t create an object indirectly.

Variable declaration represent data member of the interface and they are ment for universal common reusable and key must be initialized other wise we get compile time error.

Since data members of interface belongs to universal common reusable, whose value can be used any where and they can’t be changed (final) and whose memory space will be created only once (static) and they are universal accessiable (public) hence every data member of interface is by default belongs to public static final xxx.





Method declaration represents list of undefined method ment for performing universal common reusable operations.
To make these undefined methods as abstract explicitly we need not to use abstract keyword (by default abstract) and to make these methods universally accessible we need not to write public (by default public) hence every methods of interface by default belongs to public abstract method.

public abstract void calarea()

define an interface it with universal data members and universal methods.

//i1.java

Interface i1
{
Int a=10;
Int b=20;
void  f1();
void  f2();
}


Compile the above program

Javac  i1.java

Insure i1.java program will be compiled and generate i1.class in general if any java program is containing class definition and interface definations corresponding .class file will be generated

View the profile of i1 interface
C:/> Javap   i1

Interface  i1
{
Public static final int a;
Public static final int b;
Public abstract void f1();
Public abstract void f2();
}

Inheriting  the feature of interface
In java programming to inherit the feature of interfaces, we have three approaches they are
Approach 1
This approach makes to understand how to inherit the feature of base interfaces into derived class






Syntax:

[abstract]   class   <classname>   inplements    <intf1>,  <intf2>………….<intfn>
{
Variable    declaration;
Method  definition / declaration;
}

Explanation:    in the above syntax
   
  1.     <classname> represents name of the derived class  
  2.      <intf 1>, <intf 2>………<intf n> represents name of the base interfaces
  3.      Implements is the keyword used for inheriting the feature of base interfaces into derived class   
           it is improve the functionality of derived class.
  •       In java programming one derived class van extends only one base class and one derived class can implements either one or more than one interface.
  •     When ever the derived class inherits multiple abstract methods and if the derived class is not defining at least one abstract method than the current abstract by using abstract keyword.



 



No comments :

Post a Comment