Additional information on interfaces

  • A class can inherit (= extends) from only one class

    But: a class can implement multiple interfaces

    Example:

       public class MyApp extends App
                          implements ActionListener, KeyListener
       {
          ...    
       }

  • Important result:

    • Each interface defines a set of capabilities or "roles"

    • Implementing an interface allows a class to fulfill the role defined by an interface

    • Therefore:   a class in Java can fulfill multiple roles !

Additional information on interfaces

  • instanceof:

    • The instanceof operator tells you whether a variable "is a" member of a class or an interface

      (I.e.: an interface is similar to a (abstract) class in Java !)

  • Examples:

       Circle a      = new Circle("red", 2);
       BankAccount b = new BankAccount(100);
    
     Because Circle implements ComparableThing:
    
           a instanceof ComparableThing    ---> true
    
     Because BankAccount implements ComparableThing:
    
           b instanceof ComparableThing    ---> true
    

Quiz on instanceof

  • Suppose the class Dog is defined as:

         class Dog extends Animal implements Pet
         {
            ...
         }
    
         Dog fido = new Dog();

    What is the outcome of the following instanceof expressions:

     fido instanceof Dog
     fido instanceof Animal    
     fido instanceof Pet       
     fido instanceof Object  

Quiz on instanceof

  • Suppose the class Dog is defined as:

         class Dog extends Animal implements Pet
         {
            ...
         }
    
         Dog fido = new Dog();

    Answers: all are true...

     fido instanceof Dog      true (self)
     fido instanceof Animal   true (superclass)
     fido instanceof Pet      true (interface is like superclass)
     fido instanceof Object   true (super-superclass)

A scenario for default implementation of methods in an interface

  • You write a super cool interface.

    You provide your interface to a bunch of friends and they implement your interface with various super cool classes and deploy their code on their super cool websites....

  • Later....

    You realize you forgot to add one of your desired methods to your interface.

  • You edit your interface to add this abstract method....

    Result:

    • All of your friends' super cool classes won't compile because they haven't implemented this new method, and their websites are unusable.

Methods with a default implementation in an interface

  • You can define methods with a "default" implementation in an interface

  • Syntax to declare (abstract) methods with a default implementation:

       public interface InterfaceName
       {
           public default returnType methodName( params )
           {
               default method body
           }
       }

  • Usage:

    • When the implementing class does not override a method with a default implementation:

      • The Java compiler will use the default implementation as the overriding method

DEMO: demo/05-interfaces/30-default-impl/MyInterface.java + MyImpl1.java + Demo.java
DEMO: demo/05-interfaces/30-default-impl/MyInterface.java + MyImpl2.java (omit) + Demo2.java

Additional information on interfaces   --- interface inheritance

  • An interface can extend one or more interfaces

    Example:

       public interface Insurable extends Sellable, Transportable
       {
           public int insuredValue();
       }

    The interface Insurable will combine:

    • All methods in Sellable
    • All methods in Transportable
    • The insuredValue() method

  • Note:

    • Unlike classes that can extend only 1 class, interfaces can extend more than 1 interface

Comparing an abstract class vs an interface

 

Abstract class Interface
  • Can have constructors, instance variables, constants, abstract methods and non-abstract methods
    (i.e.: Everything)
  • Can only have abstract methods and constants
     
     
  • Is extended by a subclass that may implement the abstract methods, but does not have to.

    (If the subclass does not implement all abstract methods, it must be defined as an abstract class too)

  • Is implemented by a subclass that must implement all the abstract methods