Modifying Interfaces Without Breaking Existing Code - Java Object Oriented Design

Java examples for Object Oriented Design:interface

Introduction

Create a interface with a default method, which can be used by any class that implements the interface.

Demo Code

import java.util.List;

interface TeamType { 

    List<Player> getPlayers(); 

    void setPlayers(List<Player> players); 

    void setName(String name); 

    void setCity(String city); 

    String getFullName(); //w ww. j  a v  a  2 s.co  m

    default void listPlayers() { 
        getPlayers().stream().forEach((player) -> { 
            System.out.println(player.getFirstName() + " " + player.getLastName()); 
        }); 
    } 

} 
class Player {
  
  private String firstName;
  private String lastName;
  private String position;
  private int status = -1;
  
  public Player(){
      
  }
  
  public Player(String position, int status){
      this.position = position;
      this.status = status;
  }
  

  public String playerString(){
      return getFirstName() + " " + getLastName() + " - " + getPosition();
  }

  /**
   * @return the firstName
   */
  public String getFirstName() {
      return firstName;
  }

  /**
   * @param firstName the firstName to set
   */
  public void setFirstName(String firstName) {
      this.firstName = firstName;
  }

  /**
   * @return the lastName
   */
  public String getLastName() {
      return lastName;
  }

  /**
   * @param lastName the lastName to set
   */
  public void setLastName(String lastName) {
      this.lastName = lastName;
  }

  /**
   * @return the position
   */
  public String getPosition() {
      return position;
  }

  /**
   * @param position the position to set
   */
  public void setPosition(String position) {
      this.position = position;
  }

  /**
   * @return the status
   */
  public int getStatus() {
      return status;
  }

  /**
   * @param status the status to set
   */
  public void setStatus(int status) {
      this.status = status;
  }
}

Related Tutorials