touque.ca > Education Commons > Java > Resources

Interfaces

An interface is a template for designing classes, just as a class is a template for creating objects. An interface specifies a set of method signatures (headers) but provides no method bodies. This makes an interface an abstract specification of methods.

Every class which implements (realizes) an interface must make those abstract methods concrete: for every method signature in an interface, a realizing class will provide a method body.

“One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java must have exactly one base class, the only exception being java.lang.Object (the root class of the Java type system); multiple inheritance of classes is not allowed. A Java class may implement, and an interface may extend, any number of interfaces; however an interface may not implement an interface” (Interface. Retrieved from Wikipedia on 2013-09-18).

The creation of an interface creates a new type, and object references of that type can be created, but there can be no objects of that type. Rather the object reference will refer to an object of a class which implements the interface. For example, if the TelephoneContact class implements the Contact interface, then this is possible:

// Fragment 1—Possible:
Contact contact = new TelephoneContact(…);

but this is not:

// Fragment 2—Impossible:
Contact contact = new Contact(…);

Here’s an aide-mémoire:

Creating interfaces

By definition:

  1. the methods specified in an interface are abstract: they have no method bodies
  2. the methods specified in an interface are automatically public; you should not specify the access modifier
  3. if fields are specified in an interface, they are automatically public static final and are available to every class which realizes the interface; you should not specify the modifiers
  4. an interface may not have instance fields, because an interface cannot be instantiated
  5. against an instance (object) of a class which implements an interface, you can call
    • an interface method
    • any other method of the implementing class
    For example, if in Fragment 1 (above) the Contact interface specifies a getType method, and if the TelephoneContact class has a toString method, the following are possible method calls:
    • contact.getType();
    • contact.toString();

Using interfaces

To use an interface:

  1. import the interface
  2. specify that your class implements the interface
  3. code all of the prescribed methods
  4. if the interface implements a listener: add the listener to the relevant components

Polymorphism

“In object-oriented programming, polymorphism refers to a programming language’s ability to process objects differently depending on their data type or class” (What is polymorphism? Retrieved from Webopedia on 2013-09-18).

Consider the following:

// Fragment 3—case 1:
Contact contact = new TelephoneContact(…);
System.out.println(contact.toDisplayString());

// Fragment 3—case 2:
contact = new EmailContact(…);
System.out.println(contact.toDisplayString());

In case 1, the Java virtual machine will invoke the toDisplayString method of the TelephoneContact class. In case 2, the virtual machine will invoke the toDisplayString method of the EmailContact class. It’s “the actual type of the object [that] determines the method to be called” (Horstmann, C. [2002]. Big Java. New York: John Wiley & Sons. p. 373). This determination is called late binding, and it must be made by the virtual machine because the actual type of the object may not be known until the program runs.

For example, in Fragment 4 below, the compiler cannot know at compile time the type of the object that will be passed to the validateContact method; it can know only that the formal parameter’s object reference is of type Contact.

// Fragment 4:
public void validateContact(Contact contact)
{
   // First display the contact.
   contact.toDisplay();

   …

} // end of method validateContact(Contact contact)

Early binding occurs when the Java compiler determines which method will be invoked. For example: the String class has several substring methods, each distinguished by the number of its parameters and their types. We say that the substring method is overloaded: there are several versions with the same name (but different numbers and types of parameters). At compile time, the correct determination is unambiguous, so it can be made by the compiler.

touque.ca > Education Commons > Java > Resources

[This page last updated 2020-12-23 at 12h13 Toronto local time.]