Syntax to declare interface
interface NAME
{
final static datamembers;
methods;
}
Syntax to declare a child
class CHILD:Interface Class
{
//defination of methods of interface class
}
See the following sample program with one interface and two child implementing the interface class.
interface Area
{
double Get(double x, double y);
}
class Circle:Area
{
//overriding interface method
public double Get(double x, double y)
{
return 3.14* x * x;
}
}
class Rect:Area
{
//overriding interface method
public double Get(double x, double y)
{
return x * y;
}
}
class InterfaceDemo
{
static void Main(string[] args)
{
//creating objects of child class
Rect r = new Rect();
Circle c = new Circle();
double k = r.Get(10.2, 12.9);
double l = c.Get(5.0,0);
Console.WriteLine("Area of rect= {0}", k);
Console.WriteLine("Area of circle= {0}", l);
}
}