Java101
  • Welcome
  • Videos 🖥️
    • Video Links
  • Exercises
    • Assignment 1 - Tests - הבודק
    • Exercise 2
    • Exercise 4
  • Lesson 1 - Intro
    • MyConsole
    • Variables, Conditionals and Exercises
    • More Exercises
    • IDE Shortcuts
  • Lesson 2
    • Lesson 2 - While
  • Lesson 3 - for loops
    • for loops
  • Lesson 4 - Arrays
    • Arrays
  • Lesson 5 - Functions and Multi-dim Arrays
    • Multidimensional arrays & Functions
  • Lesson 6 - Strings, JUnit and Recursion
    • Useful String functions and more
    • Strings
    • More Exercises
  • Lesson 7
    • Recursion
    • More Exercises
  • Lesson 8 - OOP
    • Intro to Classes and Objects
    • Exercise: Creating circles
    • Lesson 9 - More OOP
  • Lesson 9 - Linked Lists
    • Linked Lists
  • Lesson 10 - Interfaces
    • Interface - Intro
    • Interfaces - Exercises
  • Lesson 11 - Test Review
    • index
Powered by GitBook
On this page
  • todays lesson
  • interface (until java 7)
  • What is the output?
  • interfaces since java 9 more complicated 🙄
  • Arraylist example

Was this helpful?

  1. Lesson 10 - Interfaces

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

Cat is playing
Cat is swimming

Dog is playing
Dog is swimming
PreviousLinked ListsNextInterfaces - Exercises

Last updated 3 years ago

Was this helpful?