0% found this document useful (0 votes)
35 views

Using Objects: Some Examples And/or Figures Were Borrowed (With Permission) From Slides Prepared by Prof. H. Roumani

This document discusses key concepts related to using objects in programming, including: 1. Objects have attributes, methods, identity, and state while classes define attributes and methods. 2. Variables of non-primitive types point to objects in memory via references rather than containing the object itself. 3. The new keyword is used to instantiate an object and invoke its constructor to initialize the object's state.

Uploaded by

lekan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Using Objects: Some Examples And/or Figures Were Borrowed (With Permission) From Slides Prepared by Prof. H. Roumani

This document discusses key concepts related to using objects in programming, including: 1. Objects have attributes, methods, identity, and state while classes define attributes and methods. 2. Variables of non-primitive types point to objects in memory via references rather than containing the object itself. 3. The new keyword is used to instantiate an object and invoke its constructor to initialize the object's state.

Uploaded by

lekan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Using Objects

Some examples and/or figures were borrowed (with permission)


from slides prepared by Prof. H. Roumani
An object has: attributes, methods, an identity,
and a state
A class has: attributes and methods
Objects with the same attributes and methods can
be replaced with a class that abstracts them:

EECS1020 F14 (Steven C.) 2


Primitives
Contains a single value
Objects
Can contain numerous attributes
Each attribute has its own value
Attributes can represent primitives or other objects

EECS1020 F14 (Steven C.) 3


Variables of non-primitive types are called
references
References hold the memory address of an
object, but not the object itself
Because it is a variable, a references can be
changed to point to a different object in
memory
However, the memory address cannot be
directly manipulated

EECS1020 F14 (Steven C.) 4


Use the keyword new to instantiate an object
(i.e., reserve memory for it)
Invoke the classs constructor to initialize the
objects state (i.e., the value of its attributes)
Constructors look like methods, but
Have no return type (not even void)
Have the same name as their class
Multiple constructors could exist for a single
class, providing differing initializations

EECS1020 F14 (Steven C.) 5


1. Locate the class

Hard Drive Class Memory (RAM)

Fraction
+numerator : long
+denominator : long

type.lib

EECS1020 F14 (Steven C.) 6


2. Declare a reference

Memory (RAM)

Main Class
+f : Fraction

Fraction Class
+numerator : long
+denominator : long

EECS1020 F14 (Steven C.) 7


3. Instantiate the class

Memory (RAM)

Main Class
+f : Fraction Object : Fraction
numerator : long = 3
denominator : long = 5
Fraction Class
+numerator : long
+denominator : long

EECS1020 F14 (Steven C.) 8


4. Assign a reference

Memory (RAM)

Main Class
+f : Fraction Object : Fraction
numerator : long = 3
denominator : long = 5
Fraction Class
+numerator : long
+denominator : long

EECS1020 F14 (Steven C.) 9



int width = 8;
int height = 5;
Rectangle3 r = new Rectangle3();
r.width = width;
r.height = height;
int rArea = r.getArea();
System.out.println(rArea);

EECS1020 F14 (Steven C.) 10


A reference can only point to one object at a
time
Multiple references can point to the same object
Example
Fraction f1;
f1 = new Fraction(3, 5);
Fraction f2;
f2 = f1; // both point to the same object
State changes via one reference affects the
object
Object changes are visible via any reference to it

EECS1020 F14 (Steven C.) 11


Memory (RAM)

Main Class
+f1 : Fraction Object : Fraction
+f2 : Fraction numerator : long = 3
denominator : long = 5

Fraction Clas
+numerator : long
+denominator : long

EECS1020 F14 (Steven C.) 12


Comparison using == operator only check
memory address, not object state
Comparison of object state requires use of
the equals() method
Example
objRef1.equals(objRef2);
Definition of object equality defined by class
implementer (in API)

EECS1020 F14 (Steven C.) 13


Memory (RAM)
Main Clas
Object : Fraction
+f1 : Fraction
+f2 : Fraction numerator : long = 3
+f3 : Fraction denominator : long = 5
+f4 : Fraction
+f5 : Fraction Object : Fraction
numerator : long = 2
denominator : long = 7
Fraction Clas
Object : Fraction
+numerator : long
+denominator : long numerator : long = 6
denominator : long = 10

EECS1020 F14 (Steven C.) 14


The equals() method
Determines equality
Default: compare memory address
The toString() method
Returns textual representation of the object
Default: object type, followed by memory address
Implicitly called by print methods
Default behaviour are typically overridden by
the class implementer

EECS1020 F14 (Steven C.) 15


Accessor methods
Allow clients to determine an objects state
Names typically begin with get
E.g., getNumerator(), getDenominator()
Mutator methods
Allow clients to change an objects state
Names typically begin with set
E.g., setFraction(long numerator, long denominator)

EECS1020 F14 (Steven C.) 16


Facilitated by using accessor and mutator
methods
Enhances encapsulation
Provides means to check and enforce pre-conditions
and post-conditions
Use of accessor and mutator
Read/write access with contracts
Use of a accessor only
Read only access with contracts
Use of a mutator only
Write only access with contracts

EECS1020 F14 (Steven C.) 17


Stored in the classs memory region, not
objects
Changes in value affect all objects of that class
Example:
Because isQuoted is static, setting it to false affects
both objects
Fraction f = new Fraction(3, 2);
f.isQuoted = true;
Fraction g = new Fraction(5, 2);
g.isQuoted = false;
System.out.println(f.toProperString());
System.out.println(g.toProperString());
Should be invoked on the class, not the object
EECS1020 F14 (Steven C.) 18
In Java, the programmer cannot remove an
object from memory
Can orphan an object by removing referent
to it
Example
Fraction x = new Fraction(3, 5);
Fraction y = x;
y = new Fraction(4, 7);
x = null;
Orphaned objects are cleared via garbage
collection

EECS1020 F14 (Steven C.) 19

You might also like