Implementing Interfaces

Contact Us or call 1-877-932-8228
Implementing Interfaces

Implementing Interfaces

A class definition may, in addition to whatever else it does, implement one or more interfaces.

Once a class states that it implements an interface, it must supply all the methods defined for that interface, complete with executable code.

  • Note: it actually does not have to implement all of them, but in that case the class cannot be instantiated - it must be declared as an abstract class that can only be used as a base class (where some derived class would then fully implement the interface).

To implement an interface:

  • Add that the class implements the interface to the class declaration.
  • Add the methods specified by the interface to the body of the class. Note that you do need to specify the access terms on methods in a class that implements an interface.
[modifiers] class ClassName implements InterfaceName {

	any desired fields

	// implement required methods
	[modifiers] returnType methodName1(arguments) {
		executable code
	}

	any other desired methods

}

It is important to note that a class may implement an interface in addition to whatever else it might do, so it could have additional fields and methods not associated with the interface.

A class may implement more than one interface - that merely adds to the list of required methods. Use a comma-separated list for the interface names.

[modifiers] class ClassName implements Interface1Name, Interface2Name {

	// must implement all methods from all implemented interfacse
}

Implementing Interfaces - Example

The complete example will use three separate files (the third file will be shown shortly):

Code Sample:

Java-Interfaces/Demos/Printable.java
public interface Printable {
  void printAll();
}

Code Sample:

Java-Interfaces/Demos/PrintableThings.java
class Person implements Printable {
  private String name = new String("Bill");
  private int age = 22;
  public void printAll() {
    System.out.println("Name is " + name + ", age is " + age);
  }
}
class Stock implements Printable {
  private String tickerSymbol = new String("XYZ");
  private int shares = 100;
  private int currentPrice = 4000; // in pennies
  public void printAll() {
    System.out.println(tickerSymbol + " " + shares +
                       " shares at " + currentPrice);
    System.out.println("Value: " + currentPrice * shares);
  }
  public void sell() {
    System.out.println(tickerSymbol + " sold");
  }
}

This file contains two classes with package access. Since the classes are not public, they can both be in the same file, and the file name does not need to match either class name. This is done purely as a convenience; it is not a good programming practice in general, but is sometimes useful if one class is highly coupled (interrelated) with the other, which is not the case here. Both classes implement the Printable interface, but are otherwise not related. Stock has another method not related to Printable.

Next