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?

  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();
      }
  }
  implemented first..
  20
  implemented second..

another example

  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

    • since java 9:

      1. static methods

      2. private methods

      3. private static methods private static void sub(int a, int b)

      4. default methods

      5. and a few more changes

example

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

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();
        }
    }
}
Dog is playing
Dog is swimming

Cat is playing
Cat is swimming

Dog is playing
Dog is swimming

Last updated