2. Outline
Classes, objects, methods and instance variables
Methods in a class (last lecture)
Creating of objects
Constructor (Default, Parameterized)
Static Members (fields, methods)
Use of this
Array as Field
Array of objects
Home Reading
The differences between instance variables of a class and local variables
of a method.
More than one classes in a program
3. Classes
A class is a collection of fields (data) and methods
(procedure or function) that operate on that data.
3
Circle
centre
radius
circumference()
area()
4. Classes
A class is a collection of fields (data) and methods
(procedure or function) that operate on that data.
The basic syntax for a class definition:
Bare bone class – no fields, no methods
4
public class Circle {
// my circle class
}
class ClassName [extends
SuperClassName]
{
[fields declaration]
[methods declaration]
}
5. Adding Fields: Class Circle with fields
Add fields
The fields (data) are also called the instance varaibles.
5
public class Circle {
public double x, y; // centre coordinate
public double r; // radius of the circle
}
6. 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
7. 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
8. Adding Methods to Class Circle
8
public class Circle {
public double x, y; // centre of the circle
public double r; // radius of circle
//Methods to return circumference and area
public double circumference() {
return 2*3.14*r;
}
public double area() {
return 3.14 * r * r;
}
}
Method Body
9. Data Abstraction
Declare the Circle class, have created a new data type
– Data Abstraction
Can define variables (objects) of that type:
Circle aCircle;
Circle bCircle;
9
10. Class of Circle cont.
aCircle, bCircle simply refers to a Circle object, not an
object itself.
10
aCircle
Points to nothing (Null Reference)
bCircle
Points to nothing (Null Reference)
null null
11. Creating objects of a class
Objects are created dynamically using the new
keyword.
aCircle and bCircle refer to Circle objects
11
bCircle = new Circle() ;
aCircle = new Circle() ;
12. 12
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
13. 13
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
P
aCircle
Q
bCircle
Before Assignment
P
aCircle
Q
bCircle
Before Assignment
14. Automatic garbage collection
The object does not have a reference and
cannot be used in future.
The object becomes a candidate for automatic
garbage collection.
Java automatically collects garbage periodically and
releases the memory used to be used in the future.
14
Q
15. Accessing Object/Circle Data
Similar to C syntax for accessing data defined in a
structure.
15
Circle aCircle = new Circle();
aCircle.x = 2.0 // initialize center and radius
aCircle.y = 2.0
aCircle.r = 1.0
ObjectName.VariableName
ObjectName.MethodName(parameter-list)
16. Executing Methods in Object/Circle
Using Object Methods:
16
Circle aCircle = new Circle();
double area;
aCircle.r = 1.0;
area = aCircle.area();
sent ‘message’ to aCircle
18. 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.
19. Passing Objects to Methods, cont.
main
method
Reference
myCircle
5
n 5
times
printAreas
method
Reference
c
myCircle: Circle
radius = 1
Pass by value (here the value is 5)
Pass by value (here the value is the
reference for the object)
Passing by value (the value is the reference to the object)
20. The Keyword this
Use this to refer to the current object.
Use this to invoke other constructors of the object.
21. 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.
22. Array of Objects, cont.
Circle[] circleArray = new
Circle[10];
reference Circle object 0
circleArray[0]
…
circleArray
circleArray[1]
circleArray[9] Circle object 9
Circle object 1