Extending the Functionality of a Class - Java Object Oriented Design

Java examples for Object Oriented Design:Inheritance

Introduction

Use the extends keyword followed by the name of the class that you would like to extend.

Demo Code

class WoodenStick extends Stick {

  private static final String material = "WOOD";
  private int lie;
  private int flex;

  public WoodenStick(int length, boolean isCurved) {
    super(length, isCurved, material);
  }/*from ww  w .j  a v  a 2  s .  c  o m*/

  public WoodenStick(int length, boolean isCurved, int lie, int flex) {
    super(length, isCurved, material);
    this.lie = lie;
    this.flex = flex;
  }

  /**
   * @return the lie
   */
  public int getLie() {
    return lie;
  }

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

  /**
   * @return the flex
   */
  public int getFlex() {
    return flex;
  }

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

class Stick {

  private int length;
  private boolean curved;
  private String material;

  public Stick(int length, boolean curved, String material) {
    this.length = length;
    this.curved = curved;
    this.material = material;
  }

  /**
   * @return the length
   */
  public int getLength() {
    return length;
  }

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

  /**
   * @return the curved
   */
  public boolean isCurved() {
    return curved;
  }

  /**
   * @param curved
   *          the curved to set
   */
  public void setCurved(boolean curved) {
    this.curved = curved;
  }

  /**
   * @return the material
   */
  public String getMaterial() {
    return material;
  }

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

}

Related Tutorials