Chapter 11
The Making of Objects
11.1 Classes
- Computational objects belong to classes
- The classes provide blue prints for introducing individual objects, and features for dealing with their objects as a
group.
class Account {
static int balance;
static void transfer(int amount){}
int name;
Account(String name){}
int loan;
void borrow (int amount){}
void pay (int amount){}
int checking;
void deposit (int amount){}
void withdraw (int amount){}
}
11.2 Access to Objects
- Objects are instantiated by constructors
- Each object is assigned a handle through which the object can be reached.
- A handle may be considered to be a unique name, address, or pointer.
- Objects may be referenced only indirectly through class variables holding their handles
- Objects not referenced by variables are treated as garbage and the system periodically takes care of discarding
them (i.e., garbage collection).
class prg{
public static void main( String [] args ){
Account acc1, acc2;
acc1 = new Account( "Joe" );
acc2 = new Account( "Jane" );
acc1 = new Account( "Tom" );
} }
11.3 Attributes of Objects
- Determined by the instance fields and their values
11.4 Behavior of Objects
- Determined by the instance methods
acc1.borrow( 100 );
acc2.pay(200 );
acc1.deposit( 300 );
acc2.withdraw( 400 );
11.5 Classes as Objects
- Classes also have names, fields, and methods
11.6 Sources of Information
- Extracted by ‘javap’ from the object code.
- Found in documentation produced by ‘javadoc’.
11.7 Assignment #12: Using Class Methods
Due: Fr, May 2, midnight
Consider the incomplete program given below.
- Introduce to ‘MyClass’: 2 class fields, 3 instance fields, 3 class methods, and 2 instance methods.
- Introduce to the ‘main’ method: 2 local variables of type ‘MyClass’, 5 objects of type ‘MyClass’, and a reference to
each of the methods of class ‘MyClass’.
class lab12{
public static void main (String [] args ){
}
}
class MyClass{
}
Notes
- Make sure that your program compiles correctly (under ‘javac’).
- Assume ‘lab12’ for the submit program. The program should be stored in a file named ‘lab12.java’.
Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade
of 0 points.
Q&A