A interface is similar to a class but you can only declare methods and variables within the interface.
Interfaces can be very useful if you want to use a method from a or several objects that may not be related(extended), this may come in handy if you you need to use many objects as its possible to implement many classes but its not possible to extend many classes..
-------------------------CODE ---------------------------------
class start
{
public static void main(String args[])
{
shopfloor employeeA = new shopfloor();
managers employeeB = new managers();
finance fina = new finance();
employeeA.shopfloor(fina);
employeeB.managers(fina);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
interface payrolInterface
{
public int wage (int hoursWorked, int hourPay);
}
//////////////////////////////////////////////////////////////////////////////////////////////
class finance implements payrolInterface
{
public int wage (int hoursWorked, int hourPay)
{
return hoursWorked * hourPay;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
class shopfloor
{
public void shopfloor(payrolInterface p)
{
int salary;
salary=p.wage(35,7);
System.out.println("Shopfloor Salalry : "+salary);
}
}
///////////////////////////////////////////////////////////////////////////////////////
class managers
{
public void managers(payrolInterface p)
{
int salary;
salary=p.wage(36,12);
System.out.println("Manager Salary : "+salary);
}
}
----------------------------------------OUTPUT ------------------------------------------
Shopfloor Salalry : 245
Manager Salary : 432
No comments:
Post a Comment