Chapter 6 Objects and Classes: OO Programming Concepts Creating Objects and Object Reference Variables
Chapter 6 Objects and Classes: OO Programming Concepts Creating Objects and Object Reference Variables
Chapter 6 Objects and Classes: OO Programming Concepts Creating Objects and Object Reference Variables
Differences between primitive data type and object type Automatic garbage collection
Instance and Class Variables and Methods Scope of Variables Use the this Keyword Case Studies (Mortgage class and Count class)
OO Programming Concepts
An object data field 1 ... data field n method 1 ... method n Behavior State
Method findArea A Circle object Data Field radius = 5
findArea(): double new Circle() circle1: Circle radius = 2 ... new Circle() circlen: Circle radius = 5
UML Graphical notation for objects
Class Declaration
class Circle { double radius = 1.0; double findArea(){ return radius * radius * 3.14159; } }
Example:
Circle myCircle;
Creating Objects
objectReference = new ClassName();
Example:
myCircle = new Circle();
Example:
Circle myCircle = new Circle();
Before: i j 1 2
After: i j 2 2
Garbage Collection
As shown in the previous figure, after the assignment statement c1 = c2, c1 points to the same object referenced by c2. The object previously referenced by c1 is no longer useful. This object is known as garbage. Garbage is automatically collected by JVM.
Accessing Objects
Referencing
objectReference.data
myCircle.radius
Invoking
objectReference.method
myCircle.findArea()
TestCircle
Run
Constructors
Circle(double r) { radius = r; Constructors are a } special kind of methods that are Circle() { radius = 1.0; invoked to construct objects. } myCircle = new Circle(5.0);
Constructors, cont.
A constructor with no parameters is referred to as a default constructor. Constructors must have the same name as the class itself.
Demonstrate using classes from the Java library. Use the JFrame class in the javax.swing package to create two frames; use the methods in the JFrame class to set the title, size and location of the frames and to display the frames.
TestFrame Run
TestCircleWithConstructors
Run
public
The class, data, or method is visible to any class in any package.
private
The data or methods can be accessed only by the declaring class. The get and set methods are used to read and modify private properties.
TestCircleWithAccessors
Run
TestPassingObject
Run
c myCircle Reference Reference Pass by value (here the value is the reference for the object)
Instance variables belong to a specific instance. Instance methods are invoked by an instance of the class.
radius
CircleWithStaticVariable
-radius -numOfObjects +getRadius(): double +setRadius(radius: double): void +getNumOfObjects(): int +findArea(): double instantiate
-radius = 1 -numOfObjects = 2
2
circle2:Circle
numOfObjects
-radius = 5 -numOfObjects = 2
radius
Scope of Variables
The scope of instance and class variables is the entire class. They can be declared anywhere inside a class.
The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used.
Array of Objects
Circle[] circleArray = new Circle[10]; An array of objects is actually an array of reference variables. So invoking circleArray[1].findArea() involves two levels of referencing as shown in the next figure. circleArray references to the entire array. circleArray[1] references to a Circle object.
circleArray
circleArray[9]
TotalArea
Run
Class Abstraction
Class abstraction means to separate class implementation from the use of the class. The creator of the class provides a description of the class and let the user know how the class can be used. The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user.
TestVoteCandidate
Run
java.lang Contains core Java classes, such as numeric classes, strings, and objects. This package is implicitly imported to every Java program.
java.io Contains classes for input and output streams and files. java.util Contains many utilities, such as date. java.net Contains classes for supporting network communications.
java.awt.image
Contains classes for managing bitmap images. java.awt.peer Platform-specific GUI implementation. Others:
java.sql java.rmi