Skip to main content
ICT
Lesson A11 - Inheritance
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

E. Interfaces page 7 of 10

  1. In Java, an interface is a mechanism that unrelated objects use to interact with each other. Like a protocol, an interface specifies agreed-on behaviors and/or attributes.

  1. The Person class and its class hierarchy define the attributes and behaviors of a person. But a person can interact with the world in other ways. For example, an employment program could manage a person at a school. An employment program isn't concerned with the kinds of items it handles as long as each item provides certain information, such as salary and employee ID. This interaction is enforced as a protocol of method definitions contained within an interface. The Employable interface would define, but not implement, methods that set and get the salary, assign an ID number, and so on.


    Figure 11.4 - Employable Interface

  2. To work in the employment program, the Teacher class must agree to this protocol by implementing the interface. To implement an interface, a class must implement all of the methods and attributes defined in the interface. In our example, the shared methods of the Employable interface would be implemented in the Teacher class.

  3. In Java, an interface consists of a set of methods and/or methods, without any associated implementations. Here is an example of Java interface that defines the behaviors of “employability” described earlier:

    public interface Employable{
      public double getSalary();
      public String getEmployeeID();

      public void setSalary(double salary);
      public void setEmployeeID(String id);
    }

    A class implements an interface by defining all the attributes and methods defined in the interface. implements is a reserved word. For example:

    public class Teacher implements Employable{
      ...
      public double getSalary() { return mySalary; }
      public int getEmployeeID() { return myEmployeeID; }

      public void setSalary(double salary) { mySalary = salary; }
      public void setEmployeeID(String id) { myEmployeeID = id; }
    }

  4. A class can implement any number of interfaces. In fact, a class can both extend another class and implement one or more interfaces. So, we can have things like (assuming we have an interface named Californian)

    public class Teacher extends Person implements Employable, Californian{
    ...
    }

  5. Interfaces are useful for the following:

    - Declaring a common set of methods that one or more classes are required to implement
    - Providing access to an object’s programming interface without revealing the details of its class.
    - Providing a relationship between dissimilar classes without imposing an unnatural class relationship.

  6. You are not likely to need to write your own interfaces until you get to the point of writing fairly complex programs. However, there are a few interfaces that are used in important ways in Java’s standard packages. You’ll learn about some of these standard interfaces in future lessons.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.