Interface

/*An interface is basically a kind of class. The difference is that interface define any abstract methods and final fields(datamember or data functions). Interfaces do not specify any code,we can declare methods and  data fields contain only constant. Therefore, it is the responsibility of the class that implements an interface to define the code for implementation of these meathods.


**interface contain a set of abstarct methods and static data members.
*Interface also known as a prototype for a class
*all methods defined in an interface are only abstract methods means we can declare these methods.
Abstarct method contains only the declaration for a method without any mplementation details.
Implementation of abstract method is defined in the classimplementing the interface.
u can implement multiple interfaces in a single class.
*/

interface Area
{
    final static float pi=3.14F;
    float comput(float x,float y);
}

class Rectangle implements Area
{
    public float comput(float x,float y)
    {
        return(x*y);
    }
}

class Circle implements Area
{
    public float comput(float x,float y)
    {
        return(pi*x*y);
    }
}

class interfacetest
{
    public static void main(String args[])
    {
        Rectangle rect = new Rectangle();
        Circle cir = new Circle();
        
        Area arrr;
        
        arrr=rect;
        System.out.println("The output (area of rectangle) rect = "+arrr.comput(20,10));
        arrr=cir;
        System.out.println(" The calculation of circle class which inherits interface(Area) is= "+arrr.comput(10,10));
    }
}

area of rectangle and circle

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @