Practice: Interfaces 101

Practice: Interfaces 101#

Question #1: Describe the difference between an interface and a class.

Click to see answer The differences between an interface and a class is summarized in this table with more a more detailed explanation below.

interface

class

Access modifier

must be public

unbounded

Abstract methods

implicitly abstract

May have abstract methods if the class is defined as abstract

Implementation allowed?

Only in static and default methods

yes

Hierarchy Keyword

extends

extends

Has fields

NO

yes

Has Constructors

NO

yes

  1. Access modifiers include public and private. All the methods in an interface must be public while a class can have any and all access modifiers.

  2. Abstract methods have no implementation. A class can have abstract methods, but then the class must be defined as abstract and cannot be instantiated directly. An interface implicitly defines all its methods as abstract. However, it is possible for an interface to also have static and default methods which have implemenations. default is a keyword that defines an instance method with implementation.

  3. Hierarchy is defined with the extends keyword for both an interface and a class. This is because an interface can inherit the implementation of default methods. The static methods are not inherited and can only be accessed via the implementating class.

  4. Fields are not allowed in interfaces and, therefore, interfaces themselves don’t have state. This gives the impression that interfaces don’t require state, however, a class that implements an interface may have state. Consider the iterator interface which as the method boolean hasNext. This means that the class that implements the interface must know the current whether another element is available. This is state.

  5. Constructors appear only in classes, not in interfaces.

  6. Implementation type is an andvanced concept. (see below) It is sufficient to know that both an interface and a class are implemented with similar, but not identical, flexibility and restrictions. They are generally defined in their own file, but this isn’t required.


Question #2: Create an interface and a class that makes a promise via the implements keyword.


Question #3: Write code that illustrates how you can get access to interface methods via casting (aka type casting). Include code that will ask an object if it implements an interface.


Question #4: Describe what the IS-A relationship means?