![Page 1: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/1.jpg)
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
Constructors Modifiers (public, private and static) Instance and Class Variables and Methods Scope of Variables Use the this Keyword Case Studies (Mortgage class and Count class)
![Page 2: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/2.jpg)
OO Programming Concepts
data field 1
method n
data field n
method 1
An object
...
...
State
Behavior
Data Field radius = 5
Method findArea
A Circle object
![Page 3: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/3.jpg)
Class and Objects
circle1: Circle
radius = 2
new Circle()
circlen: Circle
radius = 5
new Circle()
...
UML Graphical notation for classes
UML Graphical notation for objects
Circle
radius: double
findArea(): double
UML Graphical notation for fields
UML Graphical notation for methods
![Page 4: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/4.jpg)
Class Declaration
class Circle { double radius = 1.0;
double findArea() { return radius*radius*3.14159; }}
![Page 5: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/5.jpg)
Declaring Object Reference Variables
ClassName objectName;
Example:Circle myCircle;
![Page 6: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/6.jpg)
Creating Objects
objectName = new ClassName();
Example:myCircle = new Circle();
The object reference is assigned to the object reference variable.
![Page 7: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/7.jpg)
Declaring/Creating Objectsin a Single Step
ClassName objectName = new ClassName();
Example:Circle myCircle = new Circle();
![Page 8: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/8.jpg)
Differences between variables of primitive Data types and object types
1
c: Circle
radius = 1
Primitive type int i = 1 i
Object type Circle c c reference
Created using new Circle()
![Page 9: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/9.jpg)
Copying Variables of Primitive Data Types and Object Types
1
c1: Circle
radius = 5
Primitive type assignmenti = j
Before:
i
2j
2
After:
i
2j
Object type assignmentc1 = c2
Before:
c1
c2
After:
c1
c2
c2: Circle
radius = 9
![Page 10: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/10.jpg)
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.
![Page 11: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/11.jpg)
Garbage Collection, cont
TIP: If you know that an object is no longer needed, you can explicitly assign null to a reference variable for the object. The Java VM will automatically collect the space if the object is not referenced by any variable.
![Page 12: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/12.jpg)
Accessing Objects
Referencing the object’s data:
objectName.data
myCircle.radius
Invoking the object’s method:
objectName.method
myCircle.findArea()
![Page 13: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/13.jpg)
Example 6.1 Using Objects
Objective: Demonstrate creating objects, accessing data, and using methods.
TestCircleTestCircle RunRun
![Page 14: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/14.jpg)
Constructors
Circle(double r) { radius = r;}
Circle() { radius = 1.0; }
myCircle = new Circle(5.0);
Constructors are a special kind of methods that are invoked to construct objects.
![Page 15: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/15.jpg)
Constructors, cont.A constructor with no parameters is referred to as a default constructor.
Constructors must have the same name as the class itself.
Constructors do not have a return type—not even void.
Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.
![Page 16: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/16.jpg)
Example 6.2 Using Constructors
Objective: Demonstrate the role of constructors and use them to create objects.
TestCircleWithConstructorsTestCircleWithConstructors RunRun
![Page 17: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/17.jpg)
Visibility Modifiers and Accessor Methods
By default, the class, variable, or data can beaccessed by any class in the same package.
publicThe 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.
![Page 18: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/18.jpg)
Example 6.3Using the private Modifier
and Accessor Methods
TestCircleWithAccessorsTestCircleWithAccessors RunRun
In this example, private data are used for the radius and the accessor methods getRadius and setRadius are provided for the clients to retrieve and modify
the radius.
![Page 19: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/19.jpg)
Passing Objects to Methods
Passing by value (the value is the reference to the object)
Example 6.3 Passing Objects as Arguments
TestPassingObjectTestPassingObject RunRun
![Page 20: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/20.jpg)
Instance Variables, and Methods
Instance variables belong to a specific instance.
Instance methods are invoked by an instance of the class.
![Page 21: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/21.jpg)
Class Variables, Constants, and Methods
Class variables are shared by all the instances of the class.
Class methods are not tied to a specific object.
Class constants are final variables shared by all the instances of the class.
![Page 22: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/22.jpg)
Class Variables, Constants, and Methods, cont.
To declare class variables, constants, and methods, use the static modifier.
![Page 23: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/23.jpg)
Class Variables, Constants, and Methods, cont.
CircleWithStaticVariable -radius -numOfObjects +getRadius(): double +setRadius(radius: double): void +getNumOfObjects(): int +findArea(): double
1 radius circle1:Circle -radius = 1 -numOfObjects = 2
instantiate
instantiate
Memory
2
5 radius
numOfObjects
radius is an instance variable, and numOfObjects is a class variable
UML Notation: +: public variables or methods -: private variables or methods underline: static variables or metods
circle2:Circle -radius = 5 -numOfObjects = 2
![Page 24: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/24.jpg)
Example 6.5Using Instance and Class Variables
and Method
Objective: Demonstrate the roles of instance and class variables and their uses. This example adds a class variable numOfObjects to track the number of Circle objects created.
Test CircleWithStaticVariableTest CircleWithStaticVariable RunRun
![Page 25: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/25.jpg)
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.
![Page 26: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/26.jpg)
The Keyword this
Use this to refer to the current object.
Use this to invoke other constructors of the
object.
![Page 27: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/27.jpg)
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.
![Page 28: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/28.jpg)
Array of Objects, cont.
reference
Circle object 0 circleArray[0]
…
circleArray circleArray[1]
circleArray[9]
Circle object 9
Circle object 1
![Page 29: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/29.jpg)
Array of Objects, cont. Example 6.6: Summarizing the areas of the
circles
Demonstrate the roles of instance and class variables and their uses. This example adds a class variable numOfObjects to track the number of Circle objects created.
TotalAreaTotalArea RunRun
![Page 30: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/30.jpg)
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.
![Page 31: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/31.jpg)
Example 6.7 The Mortgage Class
Mortgage
-annualInterestRate: double -numOfYears: int -loanAmount: double +Mortgage() +Mortgage(annualInterestRate: double, numOfYears: int, loanAmount: double) +getAnnualInterestRate(): double +getNumOfYears(): int +getLoanAmount(): double +setAnnualInterestRate(annualInteresteRate: double): void +setNumOfYears(numOfYears: int): void +setLoanAmount(loanAmount: double): void +monthlyPayment(): double +totalPayment(): double
TestMortgageClassTestMortgageClass
RunRun
MortgageMortgage
![Page 32: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/32.jpg)
Example 6.8 The Count Class
CountCount
RunRun
TestCountTestCount
Count
-count: int -numOfCounts: int +Count() +getCount(): int +setCount(count: int): void +getNumOfCounts(): int +clear(): void +increment(): void +decrement(): void
![Page 33: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/33.jpg)
Java API and Core Java classes
java.langContains core Java classes, such as numeric classes, strings, and objects. This package is implicitly imported to every Java program.
java.awt Contains classes for graphics.
java.applet Contains classes for supporting applets.
![Page 34: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/34.jpg)
java.io Contains classes for input and outputstreams and files.
java.util Contains many utilities, such as date.
java.net Contains classes for supportingnetwork communications.
Java API and Core Java classes, cont.
![Page 35: Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and](https://reader033.vdocuments.us/reader033/viewer/2022050805/5697bf781a28abf838c81b4d/html5/thumbnails/35.jpg)
java.awt.image Contains classes for managing bitmap images.
java.awt.peer Platform-specific GUI implementation.
Others:
java.sqljava.rmi
Java API and Core Java classes, cont.