next up previous contents
Next: Inheritance 11: super 1 Up: Java Notes Previous: Inheritance: Object Class

Inheritance 10: Object


/**
 *  This code is from the book:
 *  Winder, R and Roberts, G (1998)
 *  Developing Java Software</em>
 *  John Wiley & Sons.
 *  It is copyright (c) 1997 Russel Winder
 *  and Graham Roberts.
 */

import java.util.Date;

class l1Inherit10{//ObjectMethods: extends Object by default
  private int i;
  private Date d;
  private String[] s;

  public l1Inherit10(int a, Date b, String[] c){
    i = a;
    d = b;
    s = c; //Array is not copied
  }

  //A private constructor used when cloning 
  //an object. It does no initialization as
  //cloning will overwrite any values.
  private l1Inherit10()
  { }

  //Override equals to compare the value of
  //the argument with this.
  public boolean equals(Object obj){
    //Check for testing equality with
    //self and shortcut everything else
    if (this == obj)
      return true;

    //Check that the argument object is an instance
    //of the same class, as the argument is given
    //type object not the type of this class.
    //First check if the argument is null. 
    //Note the use of the || operator which only 
    //evaluates its right hand argument
    //if the left is false.
    if ((obj == null) || 
        !(obj instanceof l1Inherit10))
      return false; //Can't be equal!

    //Cast argument back to correct type
    l1Inherit10 tmp = (l1Inherit10)obj;

    //Next compare the value of each instance variable
    //Primitive types can be compared using the
    //boolean operators
    if (i != tmp.i)
      return false;

    //objects of class types need to be compared
    //using equals unless it is sufficient that
    //both references are referencing the same object
    if (!d.equals(tmp.d))
      return false;

    //An array is an object. == or equals will
    //only check if two references reference the
    //same object, so a loop is needed to iterate
    //through each array comparing elements.
    //The arrays need to be the same length.
    if (s.length != tmp.s.length)
      return false;
    for (int i = 0; i < s.length; i++){
      if (!s[i].equals(tmp.s[i]))
        return false;
    }
    return true; //Same value
  }

  //Generate a String representation of an object
  public String toString(){
    //Use a Stringbuffer to build the String
    StringBuffer sb = new StringBuffer();
    sb.append("i= " + i + ", ");
    sb.append("d = " + d + ", " );
    sb.append("s = ");
    for (int i = 0; i < s.length; i++){
      sb.append(s[i] + " ");
    }

    return sb.toString();
  }

  //Perform a deep copy. 
  //Note that neither Dates or Strings are
  //copied using clone and constructors
  //are used instead.
  public Object clone(){
    l1Inherit10 tmp = new l1Inherit10();
    tmp.i = i;

    //Cannot clone dates, so create a new
    //Date initialized to the value of d
    //tmp.d = (Date)d.clone(); //Error
    tmp.d = new Date(d.getTime());

    //Copy array element by element, 
    //calling clone for each element.
    tmp.s = new String[s.length];
    for (int i = 0; i < s.length; i++){
      tmp.s[i] = new String(s[i]);
    }
    return tmp;
  }

  //Don't need to override this as the
  //default does the right thing. 
  //However, it will be for an example.
  public void finalize(){
    //Null all the array elements
    for (int i = 0; i < s.length; i++){
      s[i] = null;
    }
    System.out.println("Finalized");
  }

  public static void main(String[] args){
    String[] upper = {"A","B","C","D","E"};
    String[] lower = {"a","b","c","d","e"};
    l1Inherit10 a = 
      new l1Inherit10(1,new Date(),upper);
    l1Inherit10 b =
      new l1Inherit10(10,new Date(1,1,2000),lower);

    System.out.println(a.equals(a));
    System.out.println(a.equals(b));
    System.out.println(b.equals(a));
    System.out.println(b.equals(b));

    System.out.println(a);
    System.out.println(b);
    b = null;//b is no longer referenced and may
          //get garbage collected (although that
          //is very unlikely during the running of
          //this program as it uses little memory).

    //Note use of cast when using clone as it
    //returns a value of type Object
    l1Inherit10 c = (l1Inherit10)a.clone();
    System.out.println(a.equals(c));
    System.out.println(c.equals(a));
    System.out.println(a);
    System.out.println(c);
  }
}
/******** sample compilation & run *******
# javac l1Inherit10.java 
Note: l1Inherit10.java uses a deprecated API.  Recompile with "-deprecation" fo
 details.
1 warning
# javac -deprecation l1Inherit10.java 
l1Inherit10.java:125: Note: 
The constructor java.util.Date(int,int,int) has been deprecated.
      new l1Inherit10(10,new Date(1,1,2000),lower);
                         ^
Note: l1Inherit10.java uses a deprecated API.  
Please consult the documentation for a better alternative.
2 warnings
# java l1Inherit10
true
false
false
true
i= 1, d = Mon Sep 13 05:20:20 GMT 1999, s = A B C D E 
i= 10, d = Tue Jul 24 00:00:00 GMT 1906, s = a b c d e 
true
true
i= 1, d = Mon Sep 13 05:20:20 GMT 1999, s = A B C D E 
i= 1, d = Mon Sep 13 05:20:20 GMT 1999, s = A B C D E 
# 
******************************************/



Ananda Amatya
9/15/1999