Lesson 1

Interfaces, Abstract classes

Interface – a reference type allowing flexibility while keeping static typing.

I. Interfaces



// implementation
interface SomethingThatDrives {
  void drive();
}

class Auto implements SomethingThatDrives {
  public void drive() {
      System.out.println("I drive");
  }
}

public class Main {
  public static void main(String[] args) {
      SomethingThatDrives itDrives = new Auto();
      itDrives.drive();
  }
}

// Dependence between classes and interfaces:

// Interface A declaration
interface A {}

// B includes A’s methods
interface B extends A {}

// C includes A’s and B’s methods
interface C extends A, B {}

// D extends Object class
class D {}

// E extends D class
class E extends D {}
Class Interface
Contains fields, methods Contains only methods and static-final fields
Methods have to be implemented (contain their body) Cannot (almost) implement methods
Can have a constructor Cannot have a constructor
Can extends only one class (by default Object class). Can implement more than one interface (or none). Can extend more than one interface (or none).
If it implements an interface it has to provide the body for all the methods from that interface. If it extends other interface it does NOT have to provide the body for the methods from that interface.
Task 0

Create interfaces from the table below. Next create a class that implements at least 2 of those interfaces (IsGame is required).

Interface’s name Method
IsGame void play();
IsStrategic void nextTurn();
IsRPG void takeQuest();
IsShooter void shoot();
Task 1

Create an object of an interface without creating a class that implements isGame interface.


// Hint:
public class Main {
  public static void main(String[] args) {
      SomethingThatDrives itDrives = new SomethingThatDrives() {
          public void drive() {
              System.out.println("An object of an anonymous class");
          }
      };
      itDrives.drive();
  }
}

II. Abstract classes



interface SomethingThatDrives {
  void drive();
}

abstract class Car implements SomethingThatDrives {
  public void pedalToTheMetal() {
      System.out.println("I drive fast");
      drive();
  }

  abstract String brand();
}

class SpecificCar extends Car {

  public void drive() {
      System.out.println("I drive");
  }

  String brand() {
      return "Specific model";
  }
}

public class Main {
  public static void main(String[] args) {
      SpecificCar sc = new SpecificCar();
      System.out.println(sc.brand());
      sc.pedalToTheMetal();
  }
}
Class Abstract Class
Contains fields, methods Contains fields, methods
One can create an object (if a constructor is not private) One cannot create an object
Methods have to contain their body Methods does NOT have to contain their body (but they can)
Has to implement all the methods from the implemented interfaces Can implement methods from the implemented interfaces
Task 2

Create an abstract class named OnlineGame. It will require from its subclasses to contain method boolean login(String name, String password).

Its purpose is to implement method play in the way that checks if a player has logged in first. The player logs in by calling method login.

If the player has logged in, method play will print information „playing with friends”.

Task 3

Create your own interface, an abstract class that implements it and a regular class which extends the abstract one. Choose your own theme, use all the mechanics and keywords presented during the lesson.


interface Sport {
  void play();
}

class Team {
  private String name;
  public Team(String name) {
      this.name = name;
  }
  public String toString() {
      return name;
  }
}

abstract class TeamSport implements Sport {
  abstract Team teamA();
  abstract Team teamB();

  public void play() {
      System.out.println(teamA() + " plays with " + teamB());
  }
}

class Football extends TeamSport {

  private Team randomTeam() {
      return new Team("Team #" + (int)(Math.random() * 1000));
  }

  Team teamA() {
      return randomTeam();
  }

  Team teamB() {
      return randomTeam();
  }
}

public class Main {
  public static void main(String[] args) {
      Sport x = new Football();
      x.play();
  }
}