Interfaces are defined with the following syntax
[visibility] interface InterfaceName [extends other interfaces]
{
constant declarations
method declarations
}
The following program demonstrates the usage of interface. It has a constant named BORDER_WIDTH and a method named ‘draw()’. The class ‘Rectangle’ is implementing the interface and thus providing definition for the method ‘draw()’.
interface Shape
{
int BORDER_WIDTH = 5;
void draw( );
}
class Rectangle implements Shape
{
void draw()
{
…….
}
}
class Test
{
public static void main(String as[])
{
Shape s1;
s1=new Rectangle();
s1.draw();
}
}
0 comments:
Post a Comment