160 likes | 244 Views
OO Programming. Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword. OO Concepts. Casting: Casting can be used to convert one primitive data type to another. For example: double x = 3.45; float y = (float)x;
E N D
OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword
OO Concepts • Casting: • Casting can be used to convert one primitive data type to another. • For example: • double x = 3.45; • float y = (float)x; • The above example converts a double value to a float value and • assigns it to the float variable named y. • It is also possible to Cast Objects from one type to another.
OO Concepts • Casting Objects: • The relationship between the Class Object and the Class • String in an inheritance hierarchy is as follows: Object String
OO Concepts • Casting Objects: • Because the String Class and the Object Class are in the same • inheritance hierarchy a String is-a type of Object. • Because of this relationship, Java allows us to declare a String • as follows: • Object a = new String(“Hello”); • The object being referenced by ‘a’ is a String, but its type has • been downplayed.
OO Concepts • Casting Objects: • The String has been downplayed and so we cannot • use the String in its full capacity. • The following example will fail: • Object a = new String(“Hello”); • int theLength = a.length(); • Length is a method specific to the String Class. It cannot • be used because the String has been downplayed to an Object • type.
Casting Objects • Casting Objects: • If we wish to use the object reference ‘a’ to its full • capacity, we must cast the object back to its specific • type. • Object a = new String(“Hello”); • //Cast the a object reference a back to a String • String b = (String)a; • int theLength = b.length(); • It is important to understand that object casts can only be • be done within an inheritance hierarchy.
Casting Objects • The inheritance hierarchy for String: • java.lang.Object • | • +--java.lang.String • The inheritance hierarchy for Integer: • java.lang.Object • | • +--java.lang.Number • | • +--java.lang.Integer • Because Integer and String are not in the same hierarchy we • cannot cast a String to an Integer or vice versa.
OO Concepts • Casting Objects: • The Java language allows us to downplay objects to make the • language more flexible. • For example we can declare an Array as type Object and store • any kind of an object in the array: • Object[ ] a = new Object[2]; • a[0] = new String(“Hello”); • a[1] = new Integer(10);
OO Concepts • Casting Objects: • We can create methods that accept arguments of type Object. • Methods that accept Objects can accept any type of Object • such as Strings or Integers. • String a = new String(“Hello”); • Integer b = new Integer(10); • Public void add(Object aValue) { • //Method body • }
OO Concepts • Vectors: • The Vector Class is a commonly used Class from the Core • Java API. • A Vector is an indexed list of object, much like an array. • You use Vectors when you need greater flexibility • than arrays provide. • It can be found in the java.util package. • The main advantage is that a Vector can grow and shrink in size • as required, but an array has a fixed size.
OO Concepts • Vectors: • Once that array is created, it has a set size, and additional • elements cannot be added. • You may think of a vector as a dynamic array that automatically • expands as more elements are added to it. • Vectors can only accommodate objects. Primitives • are not allowed. • If you wish to store individual numbers in a Vector you will • have to wrap them in their associated wrapper Class first.
OO Concepts • Vectors: • All vectors are created with some initial capacity. • When space is needed to accommodate more elements, • the capacity is automatically increased. • Like arrays, elements in a vector have an associated index. • For these reasons vectors are commonly used in • Java programming.
OO Concepts • Vectors: • Vector a = new Vector(); • A Vector object has been created • with an initial capacity of ten and • is being referenced by the variable a. • Add a String object to the Vector • String b = new String(“Hello”); • a.addElement(Object obj); Index: 0 “Hello”
OO Concepts • Vectors: • String d = new String(“Good Day”); • a.addElement(d); • To Use an Object stored in a Vector • use the following syntax: • String e = (String)a.elementAt(0); • A cast is required to use the String in its full capacity. • Remember, all objects are downplayed when placed in a Vector. Index: 0 1 Hello Good Day
OO Concepts • Vectors & Casting: • Casts are commonly used with vectors. • When retrieving a value from a vector, its type is only known • as the generic type Object • Because of this, you must use a cast to cast it back to the type • of the object that you inserted into the vector.
Vectors • The following are some important methods we will be • using with the vector Class: • Method: Description • Vector() A constructor commonly used to • create a vector Object with an initial • capacity of 10 elements. • addElement(Object obj) Adds an object to the vector. • insertElementAt(Object Adds obj to the vector at index • obj, int index) • elementAt(int index) Returns the element at the specific index