Questions and Exercises: Interfaces (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
Trail: Learning the Java Language
Lesson: Interfaces and Inheritance
Questions and Exercises
Home Page > Learning the Java Language > Interfaces and Inheritance
Questions and Exercises: Interfaces

Questions

  1. What methods would a class that implements the java.lang.CharSequence interface have to implement?

  2. What is wrong with the following interface?
    public interface SomethingIsWrong {
        void aMethod(int aValue){
            System.out.println("Hi Mom");
        }
    }
    

  3. Fix the interface in question 2.

  4. Is the following interface valid?
    public interface Marker {
    }
    

Exercises

  1. Write a class that implements the CharSequence interface found in the java.lang package. 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.

  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.

Check your answers.

Previous page: Summary of Interfaces
Next page: Inheritance

  ATENCIÓN: La traducción de esta documentación es un esfuerzo personal y voluntario. NO es un documento oficial del propietario de la tecnología Java, Oracle, ni está patrocinado por esta empresa.

Los documentos originales y actualizados (en inglés) están disponibles en: http://docs.oracle.com/javase/tutorial/. La versión disponible en este sitio es la publicada en Marzo de 2008 (más información en: "What's new and What's Old? The History of the Tutorial").

Dirige cualquier comentario, petición, felicitación, etc. a tutorialesjava@codexion.com.

Si quieres ayudar a mantener en funcionamiento esta web, colaborar con la traducción de estos documentos o necesitas que se traduzca algún capítulo en concreto puedes invitarme a un café: