Showing posts with label Interface. Show all posts
Showing posts with label Interface. Show all posts

Sunday, 9 June 2013

Using the keyword interface, you can fully abstract a class interface from its implementations. Interfaces are similar to classes, but they lack instance variables, and their methods are declared without any body. Once, it is defined any number of classes can implements an interface. Also, one class can implement any number of interfaces.
      An interface is defined much like a class. This is the general form of an interface:
      
           access interface name
          {
           return-type method-name1(parameter-list);

            return-type method-name2(parameter-list);


             type final-varname1=value;
             
              type final-varname1=value; 
               
              }

//      A simple program to describe interface

interface Drawobject
{
 float P=3.14f;
public void Area(int x);

}
class Mcolor
{
void Setcolor(String col)
{
System.out.println("Color selected by you is  "+   col);
}
}

class circle extends Mcolor implements Drawobject
{
public void Area(int r)
{
float res=P*r*r;
System.out.println("Area of circle with r  "+   res);
}
}

class Testinterface
{
public static void main(String arg[])
{
circle obj=new circle();
obj.Setcolor("red");
obj.Area(5);
}
}


Output:




Interfaces

Using the keyword interface, you can fully abstract a class interface from its implementations. Interfaces are similar to classes, but they lack instance variables, and their methods are declared without any body. Once, it is defined any number of classes can implements an interface. Also, one class can implement any number of interfaces.
      An interface is defined much like a class. This is the general form of an interface:
      
           access interface name
          {
           return-type method-name1(parameter-list);

            return-type method-name2(parameter-list);


             type final-varname1=value;
             
              type final-varname1=value; 
               
              }

//      A simple program to describe interface

interface Drawobject
{
 float P=3.14f;
public void Area(int x);

}
class Mcolor
{
void Setcolor(String col)
{
System.out.println("Color selected by you is  "+   col);
}
}

class circle extends Mcolor implements Drawobject
{
public void Area(int r)
{
float res=P*r*r;
System.out.println("Area of circle with r  "+   res);
}
}

class Testinterface
{
public static void main(String arg[])
{
circle obj=new circle();
obj.Setcolor("red");
obj.Area(5);
}
}


Output: