6, ఏప్రిల్ 2012, శుక్రవారం

INTERFACES THIS IS SOME SAMPLE CODE FOR MY CLASS ROOM EXPLANATIONS FOR THE TOTAL CONCEPTS PLEASE REFER MY HANDOUT


PROGRAM1- A SAMPLE INTERFACE IMPLEMENTATION CONCEPT
//interface definition
interface first
{
public String message();
}

//interface implementation
public class firstimpl implements first
{
public static void main(String agrs[])
{
firstimpl f=new firstimpl();
String s=f.message();
System.out.println("THE MESSAGE FROM INTERFACE IS "+s);
}
public String message()
{
return "i am an interface method ";
}
}
PROGRAM-2  WE CAN USE INTERFACE TO REFER ITS IMPLEMENTED CLASSES
interface shape
{
double pi=3.1431;
public double volume(int a, int b);
}


//first sphere class
class sphere  implements shape
{
sphere()
{}

public double volume(int a, int b)
{
double v=(4*pi*a*a*a)/3;
return v;
 }

void display()
{
System.out.println("I AM IN THE SPHERE");
}
}

//second cone class
class cone implements shape
{
cone()
{
}

public double volume(int a, int b)
{
double v=(pi*a*a*b)/3;
return v;
}

void display()
{
System.out.println("I AM IN THE SPHERE");

}
}


public class sc
{
public static void main(String args[])
{
sphere s1= new sphere();
double d=s1.volume(1,0);
s1.display();
System.out.println("the volume of  SPHERE is "+d);

cone c= new cone();
d=c.volume(1,2);
c.display();
System.out.println("the volume of cone is "+d);
}
}

//shape s;
//s=s1;
//s=c;
if you use the interface for referencing of the implemented class methods s.volume() only works but s.display() in both classes will not work
Note: - only u are allowed to call the interface methods but not class specific additional methods 



PROGRAM-3 EXPLAINS INTERFACES ARE ALLOWED TO USE THE EXTENDS PROPERTY SO THAT THEY CAN INHERIT ONE FROM ANOTHER

interface one
{
int x=100;     //this will become constant
public void display1();
}

interface two extends one
{
int y=200;
public void display2();
public void display1();
}

class x implements two
{
public void display1()
{
System.out.println("I AM  THE FIRST INERFACE METHOD");
}
public void display2()
{
System.out.println("I AM  THE SECOND INTERFACE MEHTOD1");
}
public void display3()
{
System.out.println("I AM  THE SECOND INTERFACE MEHTOD2");
}
public void display()
{  //we are allowed to refer the values of all interfaces in the implementation class
System.out.println("THE VALUES ARE "+x+'\t'+y);
}
}

public class ximpl
{
public static void main(String args[])
{
x x1=new x();
x1.display1();
x1.display2();
x1.display3();
x1.display();
}
}

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి