interface Area   //Interface Defined
{
                final static float pi=3.14f;
                float compute(float x,float y);
}
class Rectangle implements Area              //Interface Implemented
{
                public float compute(float x,float y)
                {
                                return (x*y);
                }
}
class Circle implements Area       //Auther Implementation
{
                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 area;           //Interface Object
                                area=rect;           //area refers to rect object
                                System.out.println("Area of Rectangle = "+area.compute(10,20));             
                                area=cir;              //area refers to cir object
                                System.out.println("Area of Circle = "+area.compute(10,0));
                }
}

JAVA 29.PNG

Advertisements