Program to implement interfaces in Java

Free Java Programs

Program to implement interfaces

import java.io.*;
interface Area
{
final static float pi=3.14F;
float compute(float x,floaty);
}
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 area;
Area=rect;
System.out.println(“Area of Rectangle=” + area.compute(10,20));
Area=cir;
System.out.println(“area of circle=”+area.compute(10,0));
}
}

Output:
Area of Rectangle=200
Area of Circle=314

Post a Comment

Previous Post Next Post