CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Interfaces

Interfaces
Introduction
Simple interface

An interface is a type of class that contains only abstract methods and/or named constants.

An abstract class means that nobody can ever make a new instance of that class. So an abstract class has virtually no use, no value, no purpose in life, unless it is extended. An abstract method means the method must be overridden. You cannot have an abstract method in a non-abstract class.

Interface concepts

  • An interface defines a set of public abstract methods that can be implemented by a class.
  • The interface itself doesn't provide any code to implement the methods. Instead, it merely provides the method signatures (names and arguments).
  • A class that implements an interface must provide an implementation for each method defined by the interface.
  • An interface can also define public constants. Then, those constants are available to any class that implements the interface.

To DEFINE an interface:

public interface interfaceName
{
	public abstract void methodName();
}

Interface methods are implicitly public and abstract, so including 'public' and 'abstract' is optional.

To IMPLEMENT an interface:

public class className extends superclassName implements interfaceName {...}

With an interface, a class doesn't have to come from just one inheritance tree. A class can extend one class, and implement an interface. But another class might implement the same interface, yet come from a completely different inheritance tree.

A Printable interface that defines a print method

public interface Printable
{
    public abstract void print();
}

A Product class that implements the Printable interface

import java.text.NumberFormat;

public class Product implements Printable
{
    private String code;
    private String description;
    private double price;

    public Product(
        String code, String description, double price)
    {
        this.code = code;
        this.description = description;
        this.price = price;
    }

    // get and set methods for the fields
    public void print() // implement the Printable interface
    {
        System.out.println("Code:\t\t" + code);
        System.out.println("Description:\t" + description);
        System.out.println("Price:\t\t" + this.getFormattedPrice());
    }
}

Code that uses the print method of the Product class

Printable product = new Product("java", "Murach's Beginning Java 2", 49.50);
product.print();

Resulting output

Code:           java
Description:    Murach's Beginning Java 2
Price:          $49.50

Making an Implementing a Pet interface

public interface Pet
{
    public abstract void beFriendly();
    public abstract void play();
}

public class Dog extends Canine implements Pet
{
    public void beFriendly() {...}
    public void play() {...}

    // some overriding methods
    public void roam() {...}
    public void eat() {...}
}
Previous | Simple interface | Interfaces compared to abstract classes | Java API | Next