1. Interfaces

Interfaces are used to define a set of methods that a class must have in order to be a valid type of that class.

  1. define the interface
  2. define a class that extends that interface

First we define an interface:

In [1]:
interface Human {
    public abstract double age();
}
|  Added interface Human

What can we do with it?

Human is sorta like a type/class... we spell it with a capital letter....

In [2]:
new Human()
|  Error:
|  Human is abstract; cannot be instantiated
|  new Human()
|  ^---------^

No, you can create an instance of an interface. But you can create a class that implements it:

In [3]:
class Student implements Human {

}
|  Error:
|  Student is not abstract and does not override abstract method age() in Human
|  class Student implements Human {
|  ^-------------------------------...

...but only if you satisfy the requirements of the interface:

In [4]:
class Student implements Human {
    public double age() {
        return 19.0;
    }
}
|  Added class Student

Now, you can use the class as you would normally:

In [5]:
Student student = new Student();
|  Added variable student of type Student with initial value Student@4ca8195f

In [6]:
student.age();
|  Expression value is: 19.0
|    assigned to temporary variable $4 of type double

Out[6]:
19.0

In addition, you can "cast" a class as an interface. If you do that, you can only use methods that exist in Human:

In [7]:
Human human = student;
|  Added variable human of type Human with initial value Student@4ca8195f

You can also cast it explicitly like: ((Human)student)

In [8]:
human.age()
|  Expression value is: 19.0
|    assigned to temporary variable $6 of type double

Out[8]:
19.0
In [8]:
class Student implements Human {
    public double age() {
        return 19.0;
    }
    public boolean in_cs206() {
        return true;
    }
}
|  Replaced class Student
|    Update replaced variable student, reset to null
|    Update overwrote class Student

In [9]:
Student student = new Student();
|  Modified variable student of type Student with initial value Student@d70c109

In [12]:
student.in_cs206();
|  Expression value is: true
|    assigned to temporary variable $9 of type boolean

Out[12]:
true
In [13]:
class Teacher implements Human {
    public double age() {
        return 52.0;
    }
    public boolean has_tenure() {
        return true;
    }
}
|  Added class Teacher

In [15]:
Teacher teacher = new Teacher();
|  Added variable teacher of type Teacher with initial value Teacher@2d6a9952

In [16]:
teacher.age()
|  Expression value is: 52.0
|    assigned to temporary variable $12 of type double

Out[16]:
52.0
In [17]:
ArrayList<Human> alist = new ArrayList<Human>();
|  Added variable alist of type ArrayList<Human> with initial value []

In [18]:
alist.add(teacher);
|  Expression value is: true
|    assigned to temporary variable $14 of type boolean

Out[18]:
true
In [19]:
alist.add(student);
|  Expression value is: true
|    assigned to temporary variable $15 of type boolean

Out[19]:
true
In [20]:
for (Human human: alist) {
    printf("Age: %s\n", human.age());
}
Age: 52.0
Age: 19.0

In [21]:
Human human = teacher;
|  Modified variable human of type Human with initial value Teacher@2d6a9952

In [23]:
teacher.has_tenure()
|  Expression value is: true
|    assigned to temporary variable $18 of type boolean

Out[23]:
true

See page 6 of your textbook for more information.