Considering JAVA:
1. An interface is implicitly abstract. You do not need to use the abstract
keyword when declaring an interface (although it is acceptable to use it).
2. Each method in an interface is also implicitly abstract, so the abstract
keyword is not needed. You can explicitly declare a method in an interface
as abstract, but typically the abstract keyword is left off.
3. Methods in an interface are implicitly public. It is common practice to
use the public keyword when writing an interface, but if you do not
explicitly declare a method in an interface as public, it will be public
anyway.
4. Attributes should be declared as static or final.
Abstract class has is-a relationship with the child class.
Interface has can-implement-this-functionality relationship with the child class which is implementing it.
An interface can be used
1. to expose certain behaviors of a class, without exposing all of the behaviors of a class.
2. to force behavior on other objects, ensuring that certain methods are implemented by an object.
Interfaces can be used for polymorphism reasons, since an object can take on the form of an interface type.
Exposing certain behaviors
Let's have a class Employee with various attributes and behaviors relevant to the same
Code:
public class Employee
{
private String name, address;
private double weeklyPay;
public Employee(String name, String address)
{
this.name = name;
this.address = address;
}
public String getName()
{
return name;
}
public void setName(String n)
{
name = n;
}
public String getAddress()
{
return address;
}
public void setAddress(String a)
{
address = a;
}
public double getWeeklyPay()
{
return weeklyPay;
}
public void computePay(int hoursWorked)
{
weeklyPay = hoursWorked * 6.50;
System.out.println(“Weekly pay for “ + name
+ “ is $” + weeklyPay);
}
public void mailCheck()
{
System.out.println(“Mailing check to “ + name
+ “ at “ + address);
}
}
Now consider that there are two other departments:
Payroll Department for handling the weekly payroll duties, and a
Human Resources Department for managing general information about employees
The Payroll Department does not need to access or change the personal information about an employee. The Human Resources Department needs access to an employee’s information, but should not be accessing any paycheck details.
We will expose the methods relevant to both on separate interfaces:
For Payroll
Code:
public interface Payable
{
public void computePay(int hoursWorked);
public void mailCheck();
public double getWeeklyPay();
}
For HR
Code:
public interface EmployeeInfo
{
public String getName();
public void setName(String n);
public String getAddress();
public void setAddress(String a);
}
Now let the Employee class implement both of these interfaces:
Code:
public class Employee [B]implements payable, EmployeeInfo[/B]
{
private String name, address;
private double weeklyPay;
public Employee(String name, String address)
{
this.name = name;
this.address = address;
}
public String getName()
{
return name;
}
public void setName(String n)
{
name = n;
}
public String getAddress()
{
return address;
}
public void setAddress(String a)
{
address = a;
}
public double getWeeklyPay()
{
return weeklyPay;
}
public void computePay(int hoursWorked)
{
weeklyPay = hoursWorked * 6.50;
System.out.println(“Weekly pay for “ + name
+ “ is $” + weeklyPay);
}
public void mailCheck()
{
System.out.println(“Mailing check to “ + name
+ “ at “ + address);
}
}
Now, lets have a PayableDemo class which will be only able to access behaviors relevant to it (no employee personal information should be accessed):
computePay(int hoursWorked);
void mailCheck();
getWeeklyPay();
Code:
public class PayableDemo
{
public static void main(String [] args)
{
Payable e = new Employee(“George Washington”, “Mt. Vernon”);
payroll.computePay(30);
payroll.mailCheck();
}
}
And another class HRDemo, which will be able to access only these behaviors:
getName();
setName(String n);
getAddress();
setAddress(String a);
Code:
public class HRDemo
{
public static void main(String [] args)
{
EmployeeInfo e = new Employee(“George Washington”, “Mt. Vernon”);
e.getName();
e.getAddress();
}
}
Forcing Behavior on Class
Typical examples are Mouseevent listeners etc.
Note: A long post but cleared my doubts too. Excellent question OP.