1-3-Objects Classes
1-3-Objects Classes
Reference types and their characteristics Class Definition Constructors and Object Creation Special objects: Strings and Arrays
OOAD 1998/99 Claudia Niedere, Joachim W. Schmidt Software Systems Institute [email protected] http://www.sts.tu-harburg.de
Reference Types
Predefined or user-defined Classes String Array The values of reference types are not manipulated directly. Instead handles (references) to the actual values are used.
0 0
Assigning values of other variables means assigning the references! The value of the object does not change
a = b; a b
0 0 3 4
4
Comparing Objects
Comparing two reference type variables means comparing the references they contain and, thus, comparing the identity of the referenced objects.
a b c
3 4
a == b
3 4
a != c
d == null
Data members
class DataOnly { int i; float f; boolean b; }
Each instance of DataOnly gets its own copy of the data members In a class, primitives get default values.
OOAD98/99-STS-Objects and Classes 7
Nuance
We can deduce meaning from context Wash the shirt Wash the car Wash the dog Not shirtWash the shirt carWash the car dogWash the dog
11
Method Overloading
class SuperWasher{ void wash(Shirt s) { ... } void wash(Car c) { ...} void wash(Dog d) { ...} ... }
One word, many meanings: overloaded Unique argument type combinations distinguish overloaded methods
OOAD98/99-STS-Objects and Classes 12
13
Default Constructor
Constructors are needed to create instances (objects) of a class Compiler provides one for you if you write no constructor
class Bird { int i; } public class DefaultConstructor { public static void main(String args[]) { Bird nc = new Bird(); // default! } }
OOAD98/99-STS-Objects and Classes 14
Constructor Definition
class Rock { Rock() { // This is the constructor System.out.println("Creating Rock"); } } public class SimpleConstructor { public static void main(String args[]) { for(int i = 0; i < 10; i++) new Rock(); } }
15
Constructor Overloading
Like methods constructors may be overloaded
class Tree { int height; Tree() { System.out.println("A seedling"); height = 0; } Tree(int i) { System.out.println("A new Tree, " + i + " feet tall"); height = i; } }
16
this in Constructors
A very common kind to use this is in constructors to initialize data members with the constructor's arguments
public class Animal { private int numberOfLegs; Animal(int numberOfLegs) { this.numberOfLegs = numberOfLegs; } }
17
Member Initialization
void f() { int i; // No initialization i++; }
Produces compile-time error Inside class, primitives are given default values if you don specify values t
class Data { int i = 999; long l; // defaults to zero // ...
18
Constructor Initialization
Order of initialization Order that data members are defined in class Static data initialization
class Cupboard { Bowl b3 = new Bowl(3); static Bowl b4 = new Bowl(4); // ...
b4 only created on first access or when first object of class Cupboard is created
19
Creates a handle, not the array. Can size it. t To create an array of primitives:
int [] a1 = { 1, 2, 3, 4, 5 };
Bounds are checked, length produces size of the array a1.length If you do anything wrong either the compiler will catch it or an exception will be thrown
20
10
Arrays of Objects
An array of class objects:
Animal[] a = new Animal[20]; System.out.println(a.length + " animals"); for(int i = 0; i < a.length; i++) { a[i] = new Animal((i % 2 + 1) * 2);
Can also use bracketed list (The size is then fixed at compile-time)
Integer[] a = { new Integer(1), new Integer(2), new Integer(3), };
21
Multi-dimensional Arrays
It is possible to define multi-dimensional arrays.
int[][] a;
It can also be done by nested iterations over the array and its components.
22
11
Strings
Strings are immutable objects of the class String. String literals are zero, one or more characters included within double quotes. When a binding to a string literal is executed for the first time, a new String object is created. If any other bindings to this literal appear, the respective variables will hold reference to the same object.
String a = "abc"; String b = "abc"; a b
OOAD98/99-STS-Objects and Classes
"abc"
23
String Constructors
String objects can also be created by calling a constructor. String constructors create new objects whenever they are called.
String c = new String("abc"); a
"abc"
b c
"abc"
12
String Concatenation
Strings can be concatenated by using +.
String c = "A " + "concatenation";
The concatenation also creates a new String object. Values of other types can be concatenated to strings, too. They are implicitly converted to String.
String n = "Number " + 49;
25
26
13