Sunday, April 1, 2012

Interfaces:


Interfaces:
=============
An interface is basically a kind of class.  Like classes, interfaces contain methods and variables but with a major difference.  The difference is that interfaces define  only abstract methods and final fields.  This means that interfaces do not specify any code to implement these methods and data fields contain only constants.  Therefore, it is the responsibility of the class that implements an interface to define the code for implementation of these methods.

The general form of an interface definition is:
          interface interfacename
          {
                   variable declaration;
                   method declaration;
          }

          ex:
                   interface Item{
                             static final int code=101;
                             static final String name="Fan";
                             void display();
                   }
Extending Interface
=======================
          Like classes, interfaces can also be extended. That is an interface can be subinterfaced from other interfaces.  The new subinterface will inherit all the memebers of ther superinterface in the manner similar to subclasses.
          ex:
                   interface name2 extends name1
                   {
                             body of name 2
                   }
          ex:
                   interface ItemConstants
                   {
                             int code=101;
                             String name="Fan";
                   }
                   interface Item extends ItemConstants
                   {
                             void display();
                   }

Interface cannot extend classes.  This would violate the rule that an interface can have only abstract methods and constants.

Implementing Interfaces:
========================
Interfaces are used as "superclasses" whose properties are inherited by classes.  It is therefore necessary to create a class that inherits the given interface.

class classname implements interfacename
{
          body of classname
}

When a class implements more than one interface, they are separated by a comma.
ex:
class classname extends superclass implements interface1,interface2,..........
{
          body of classname
}

Note:
          Method declaration in the interface should be defined in the class prefixed by the keyword public.

ex:
interface methods
{
          void show1();
          void show2();
}
class inter1 implements methods
{
          public void show1()
          {
                   System.out.println("Hai");
          }
          public void show2()
          {
                   System.out.println("Hello");
          }
}
          class InterTest
          {
                   public static void main(String args[])
                   {
                             inter1 i1=new inter1();
                             methods m1;
                             m1=i1;
                             m1.show1();
                             m1.show2();
                   }
          }
------------------
Interfaces can be extended

import java.io.*;
interface method1
{
          void get1();
          void put1();
}
interface methods extends method1
{
          void show1();
          void show2();
}
class inter1 implements methods
{
          String name;
          public void show1()
          {
                   System.out.println("Hai");
          }
          public void show2()
          {
                   System.out.println("Hello");
          }
          public void get1()
          {
                   name="XXXX";
          }
          public void put1()
          {
                   System.out.println(name);
          }
}
class InterTest1
{
          public static void main(String args[])throws IOException
          {
                   inter1 i1=new inter1();
                   methods m1;

                   m1=i1;

                   m1.show1();
                   m1.show2();
                   m1.get1();
                   m1.put1();
          }
}

=====================
interface Area
{
          final static float pi=3.14f;
          float compute(float x,float y);
}
class Rectangle implements Area
{
          public float compute(float x,float y)
          {
                   return (x*y);
          }
}
class Circle implements Area
{
          public float compute(float x,float y)
          {
                   return (pi*x*x);
          }
}
class InterfaceTest
{
          public static void main(String args[])
          {
                   Rectangle rect=new Rectangle();
                   Circle cir=new Circle();

                   Area a; //Interface object
                   a=rect;//area refers to rect object

                   System.out.println("Area of Rectangle="+a.compute(10,20));

                   a=cir;//area refers to cir object

                   System.out.println("Area of Circle="+a.compute(10,0));
          }
}



No comments:

Post a Comment