Interfaces

The term interface is a general term, widely used in several different areas, not only in Computer Science. For example, a graphical user interface (GUI) is a series of screens with buttons, textboxes and other elements which you interact with when running an application such as Microsoft Word. The graphical user interface is just that, an interface.

We have also seen the term interface used in a different context, during Week 2, when we talked about an interface of a class. Through the class interface you can specify the public attributes and behaviors of a class, which are presented to the user (client) of the class. This set of attributes and behaviors comprise the protocol by which objects communicate with each other. Now, I’m sure you will be content to hear that there is yet another definition of the word interface, as it applies to object-oriented development!

There are several instances in object-oriented software development where it is necessary to specify general behavior that different classes should adhere to. In addition, we do not want to specifying the details of what the behavior is because it will be different for each class. Each classes is responsible for providing a particular definition of the behavior and it is mandatory that all specified general behaviors be explicitly defined by each involved class.

For example, let’s imagine a company where there are several guidelines, one of them being the dress code of the personnel. Each department should establish its own dress code policy. It doesn’t matter what the specific dress code policy is, as long as all departments have some dress code policy. The general behavior is to enforce a dress code. Each department is responsible for determining how to enforce their specific dress code.

One way this is enforced in object-oriented languages is through the use of interfaces. In Java, an interface resembles a class (it is not a class, we cannot instantiate objects of this type), which can only contain methods signatures and constant values. Given our dress code example above, let’s look at an example of the syntax behind an interface.

The keyword interface is used followed by the interface name. The body of the interface lies between opening and closing braces: public interface iCompanyPolicies { //the name usually begins with a lowercase ‘i’

interface ICompanyPolicies { //method declarations (notice that no implementation is given) void dressCode(); }

}

If a class chooses to conform to this interface, we say that it implements the interface. To specify that a class implements an interface in Java, we use the following syntax. public class AccountingDepartment implements iCompanyPolicies{

}

All classes that implement an interface, must provide implementation details for all interface methods. Therefore, in our example above, the AccountingDepartment class needs to implement the interface method dressCode(). If the interface contained other methods as well, then the AccountingDepartment class must provide an implementation for each and every one of them. public class AccountingDepartment implements iCompanyPolicies{

//Implementation of the Interface method DressCode() void dressCode() { System.out.println("A suit with a tie is required for men."); System.out.println("A skirt and blouse is required for women."); } }

If other classes in the above scenario, such as PublicRelationsDepartment or InformationTechnologyDepartment choose to adhere to the specifications of this interface, they need to implement the iCompanyPolicies interface in a similar fashion. So as you might guess, an interface is a way to enforce structure in the classes that implement the interface. By knowing that a class implements the iCompanyPolicies interface, you are sure that it provides a method called dressCode().

Once we talk about inheritance next week and abstract classes during week 6, you will have a chance to compare what you learned about interfaces and detect similarities/differences as well as think about scenarios where one can be used over the other.