CMPUT 114 - INTRODUCTION TO COMPUTING SCIENCE

Sample Code

Last updated on: Sunday January 21st 2001



/*

  C. Jones Winter 2001 - Example of polymorphism: 

  A Vector can store Object references. Because all Java objects are 

  subclasses of the Object class, all objects can be stored in a Vector

  note:  instanceof is a reserved word in Java, used to determine if a 

  variable is bound to an object of a particular class: returns a boolean

*/



import java.awt.Point;

import java.util.*;





public class Example {

   public static void main(String args[]) {

	 Vector  aVector = new Vector();

	 Integer number = new Integer(100);

	 aVector.addElement(number);

	 Point aPoint = new Point(10, 20);

	 aVector.addElement(aPoint);

	 Date aDate = new Date("10/12/1999");

	 aVector.addElement(aDate);

	

	 int temp, count;

	 Object something;

	 for (count = 0; count < 3; count++) {

		something = aVector.elementAt(count);

		if (something instanceof Integer) {

			temp = ((Integer) something).intValue() * 2;

			System.out.println(temp);

		}

		else

			System.out.println(something);

	}

  }

}