# Interface - Intro

### todays lesson

* interfaces (before and after java 9)
  * `interface`, `implements`
* `instanceof`
* Comparable and `compareTo`
  * `Arrays.sort()`
  * `Collections.sort()`

## interface (until java 7)

* use keyword `implements` to implement the interface
* need to implement **all** methods of the interface
* you **can't** create an object of type interface (since its abstract)
* a class can implement **multiple** interfaces or just a single interface
* Interface methods are by default `abstract` and `public`
* Interface attributes are by default `public`, `static` and `final`
* An interface cannot contain a constructor (as it cannot be used to create objects)

### What is the output?

{% tabs %}
{% tab title="Main" %}

```java
  public class Main {
      public static void main(String[] args) {
          MyClass obj = new MyClass();

          int res = obj.first_method(10);
          System.out.println(res);
          obj.second_method();
      }
  }
```

{% endtab %}

{% tab title="MyClass " %}

```java
  interface IFirst {
      public int first_method(int num); // interface method
  }

  interface ISecond {
      void second_method(); // interface method
  }

  class MyClass implements IFirst, ISecond {
      public int first_method(int number) {
          System.out.println("implemented first..");
          return number + 10;
      }
      public void second_method() {
          System.out.println("implemented second..");
      }
  }
```

{% endtab %}
{% endtabs %}

```
  implemented first..
  20
  implemented second..
```

another example

```java
  interface IFirst {
      public int first_method(int num); // interface method
  }


  public class Main {
      public static void main(String[] args) {

          //IFirst first = new IFirst(); //doesn't compile, didn't implement method/s

          //implemented the interface
          //Anonymous inner class
          IFirst first = new IFirst() {
              @Override
              public int first_method(int num) {
                  return num+20;
              }
          };

          int res = first.first_method(10);
          System.out.println(res);
      }
  }
```

```
  30
```

### interfaces since java 9 more complicated 🙄

add the capabilities of the previous

* **access modifiers**
  * before java 9:  `public` but not `private`, `protected`&#x20;
  * since java 9:&#x20;
    1. **static** methods&#x20;
    2. **private** methods&#x20;
    3. **private** **static** methods  `private static void sub(int a, int b)`
    4. **default** methods
    5. and a few more changes

example

```java
interface IFirst {
    public default int first_method(int num){
        System.out.println("implemented first..");
        return add_15(num);
    }

    //can't use default with private
    private int add_15(int num){
        return num + 15;
    }
}

interface ISecond {
    void second_method(); // interface method

    public static void second_method_static(int num) {
        System.out.println(num + 20);
    }
}

class MyClass implements IFirst, ISecond {
    MyClass() {
        System.out.println("created MyClass object...\n");
    }
    public void second_method() {
        System.out.println("implemented second..");
    }
}

public class MainClass {
    public static void main(String[] args) {

        MyClass obj = new MyClass();

        System.out.println(obj.first_method(15));
        obj.second_method();

        ISecond.second_method_static(20);
        //but no MyClass.second_method_static(20)
    }
}
```

```
created MyClass object...

implemented first..
30
implemented second..
40
```

## Arraylist example

{% tabs %}
{% tab title="Main" %}

```java
class Main {
    public static void main(String[] args) {
        ArrayList<Animal> list = new ArrayList();

        list.add(new Dog());
        list.add(new Cat());
        list.add(new Dog());

        for (int i = 0; i < list.size(); i++) {
            list.get(i).play();
            list.get(i).swim();
            System.out.println();
        }
    }
}
```

{% endtab %}

{% tab title="Animal " %}

```java
import java.util.ArrayList;

//implement the Swimmer and the Player interfaces
interface Animal  {
    public void swim();
    public void play();
}

class Dog implements Animal {
    //Override the swim() and the play() methods
    public void swim(){
        System.out.println("Dog is swimming");
    }
    public void play(){
        System.out.println("Dog is playing");
    }
}


class Cat implements Animal {
    //Override the swim() and the play() methods
    public void swim(){
        System.out.println("Cat is swimming");
    }
    public void play(){
        System.out.println("Cat is playing");
    }
}
```

{% endtab %}
{% endtabs %}

```
Dog is playing
Dog is swimming

Cat is playing
Cat is swimming

Dog is playing
Dog is swimming
```
