Onwards into the Interfaces and Inheritance tutorial, starting with Interfaces. This is a short one, starting with an overview and moving onto the more interesting topic of how to evolve or update interfaces.

  • Reference type
  • Only contain constants, method signatures (abstract method), default methods, static methods and nested types.
  • Cannot be instantiated, only implemented or extended.
  • Used in APIs.
  • A class can only extend 1 class but and an interface can extend many interfaces.
  • A class can  implement many interfaces.
public interface OperateCar extends AnOtherInterface {
  // constant declarations are implicitly public, static and final
  double x = 100;
  // method signatures
  int aMethod(Type anArg);
}

public class OperateJaguar extends OneClass implements OperateCar,
                                              ManyInterfaces {
    int aMethod(Type anArg) {
  }
}
  • A reference variable can be an interface type as long as the object assigned to it is an instance of a class that implements the interface.
AnInterface ob1 = (AnInterface)obj1; // Cast obj1 to type AnInterface
  • All method declarations in an interface are implicitly public.

Evolving Interfaces

Changing an interface by adding a method will break implementing classes. Assuming you cannot anticipate all uses from the outset your options are:

  1. Create a new interface that extends the old one.
    public interface newInterface extends oldInterface {
      void newMethod();
    }
  2. Define new methods as default methods.
    public interface oldInterface {
      // Implementing classes do not need to recompile, binary compatible
      default void newMethod() {
        // must provide implementation for default methods
      };
    }

    If you extend an interface, you can do 3 things with the default method:

    • Do nothing so it is inherited.
    • Redeclare the default method and make it abstract.
    • Redefine the default method.

     

  3. Define new methods as static methods.
     public interface oldInterface {
      // Implementing classes do not need to recompile
      static void newMethod() {
        // must provide implementation for default methods
      };
    }

 

Exercises

Questions

  1. What methods would a class that implements the java.lang.CharSequence interface have to implement? Abstract methods:
    • char charAt (int index) : Returns the char value at the specified index.
    • int length() : Returns the length of this character sequence.
    • CharSequence subSequence(int start, int end) : Returns a CharSequence that is a subsequence of this sequence.
    • String toString() : Returns a string containing the characters in this sequence in the same order as this sequence
  2. What is wrong with the following interface? Only a default or static method can have an implementation. An abstract method cannot have an implementation.
    public interface SomethingIsWrong {
        void aMethod(int aValue){
            System.out.println("Hi Mom");
        }
    }
    
  3. Fix the interface in question 2.
    public interface SomethingIsWrong {
        static void aMethod(int aValue){
            System.out.println("Hi Mom");
        }
    }
  4. Is the following interface valid? Yes
    public interface Marker {
    }
    

Exercises

  1. Write a class that implements the CharSequence interface found in the java.lang package. Your implementation should return the string backwards. Select one of the sentences from this book to use as the data. Write a small main method to test your class; make sure to call all four methods. The tutorial example is quite complicated, mine isn’t.
  2. Suppose you have written a time server that periodically notifies its clients of the current date and time. Write an interface the server could use to enforce a particular protocol on its clients.
    public interface TimeUpdate {
     LocalDate getDate();
     LocalTime getTime();
     LocalDateTime getDateTime();
    }

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s