Professional Documents
Culture Documents
Post EECS2030A L6 ClassesMethodsObjectsX
Post EECS2030A L6 ClassesMethodsObjectsX
EECS 2030
F 2020 :: Section C
EECS 2030
Lecture 6 :: CLASS BASICS (constructor, method)
1
2020-09-30
▪ Classes
o class basics
Last lecture
o encapsulation
o fields
o constructors
today
o methods
Recall:
Organization of a Typical Java Program
• one or more files
package 2030.lab0; • zero or one package
import java.util;
name
import java.util; • zero or more import
statements
Class Student {
• one class
int age;
• zero or more fields
(attributes)
public Student()
{…} • zero or more constructors
public int getAge() • zero or more methods
}
{…}
2
2020-09-30
Recall:
()
color
• Class:
▪ A class is a model of a thing or concept
▪ A class bundles together a set of values (data), and valid
operations for these values (methods), into a single unit
data methods
(attributes) (behaviors)
q
Imagine we have a class Point2D that can be used to create
-3
instances that represent a location (x, y) where x and y are 8
integers
1
public static void main(String[] args) { 2
p
3
2020-09-30
1 5
Point2D p = new Point2D(1, 2); q -3
8
2 p 13 r
Point2D q = new Point2D(-3, 8);
Point2D r = new Point2D(5, 13);
600 Point2D object
x 1
Point2D(1,2)
y 2
64 client
p 600a
700 Point2D object
q 700a
Point2D(-3,8) x -3 GCH
r 800a
y 8
1 5
Point2D p = new Point2D(1, 2); q -3
8
2 p 13 r
Point2D q = new Point2D(-3, 8);
Point2D r = new Point2D(5, 13);
600 Point2D object
x 1
Point2D(1,2)
y 2
64 client
p 600a
700 Point2D object
q 700a
Point2D(-3,8) x -3 GCH
r 800a
y 8
4
2020-09-30
1 5
Point2D p = new Point2D(1, 2); q -3
8
2 p 13 r
Point2D q = new Point2D(-3, 8); w
Point2D r = new Point2D(5, 13);
600 Point2D object
x 1
Point2D(1,2)
y 2
64 client
p 600a
700 Point2D object
q 700a
Point2D(-3,8) x -3 GCH
r 800a
y 8
w 600a
Point2D w = p;
1 5
Point2D p = new Point2D(1, 2); q -3
8
2 p 13 r
Point2D q = new Point2D(-3, 8); w
Point2D r = new Point2D(5, 13);
600 Point2D object
x 1
Point2D(1,2)
y 2
64 client
p 600a
700 Point2D object
q 700a
Point2D(-3,8) x -3 GCH
r 800a
y 8
w 600a
Point2D w = p;
p = null; // object at 600 not unreachable, will not be garbage collected
‘reference count’ is not 0, as w reference it
10
5
2020-09-30
r
11
11
Recall:
Organization of a Typical Java Program
• one or more files
package 2030.lab0; • zero or one package
import java.util;
name
import java.util; • zero or more import
statements
Class Student {
• one class
int age;
• zero or more fields
public student()
(attributes)
{…} • zero or more constructors
public int getAge() • zero or more methods
} {…}
12
12
6
2020-09-30
Implementing classes ()
() color
13
/**
* A simple class for representing points in 2D Cartesian
* coordinates. Every <code>Point2D</code> instance has a
* public x and y coordinate that can be directly accessed
* and modified.
*
* @author EECS2030
*/
package eecs2030.basics;
14
7
2020-09-30
Using Point2D
15
Encapsulation
• we can add features to Point2D to make it easier to use
▪ we can add access modifiers to fields to prevent unauthorized
access or manipulation of data (information hiding)
▪ we can (need to) add methods that use the fields of Point2D to
perform some sort of computation
o e.g., calculate distance between two points, or distance to origin
o e.g., translate, rotate or scale a point (w.r.t. an origin)
16
16
8
2020-09-30
Encapsulation
color
17
17
Access Modifiers
18
18
9
2020-09-30
19
19
20
20
10
2020-09-30
Default (package) access, not visible outside package (even after import)
21
21
Private: The access level is only within the class -- cannot be accessed
from outside the class..
22
11
2020-09-30
23
/**
* A better way to implement fields of Point2D
*/
package eecs2030.basics;
24
24
12
2020-09-30
package eecs2030.basics;
p.x = -1.0f;
p.y = 1.5f;
System.out.println("p = (" + p.x + ", " + p.y + ")");
25
25
Encapsulation
• we can add features to Point2D to make it easier to use
▪ we can add access modifiers to fields to prevent unauthorized
access or manipulation of data (information hiding)
26
26
13
2020-09-30
Recall:
Organization of a Typical Java Program
• one or more files
package 2030.lab0; • zero or one package
import java.util;
name
import java.util; • zero or more import
statements
Class Student {
• one class
int age;
• zero or more fields
public student()
(attributes)
{…} • zero or more constructors
}
public int getAge() • zero or more methods
{…}
27
27
28
Stopped here last time
28
14
2020-09-30
Constructors
• the purpose of a constructor:
▪ to initialize the state of an object
o it should set the values of all of the non-static fields to
appropriate values
No constructors defined?
30
30
15
2020-09-30
31
31
/**
* The default constructor. Sets the x and y coordinate
* of the point to -1.0 and 0.0f.
*/
public Point2D() {
x = -1.0f;
y = 0.0f;
}
//or better:
32
16
2020-09-30
/**
* The default constructor. Sets both the x and y coordinate
* of the point to 0.0f.
*/
public Point2D() {
this.x = -1.0f;
this.y = 0.0f;
}
33
33
Sometimes necessary:
e.g., to resolve ambiguity between instance variables and
parameters
34
34
17
2020-09-30
/**
* The default constructor. Sets both the x and y coordinate
* of the point to 0.0f.
*/
public Point2D() {
// in main of client class:
this.x
p1. = -1.0f;
p1.
this.y = 0.0f; Point2D p1 = new Point2D();
}
35
35
/**
* The default constructor. Sets both the x and y coordinate
* of the point to 0.0f.
*/
public Point2D() {
// in main of client class:
p2.
this.x = -1.0f;
p2.
this.y = 0.0f; Point2D p1 = new Point2D();
}
Point2D p2 = new Point2D();
36
36
18
2020-09-30
2. Custom constructors
• a class can have multiple constructors but the signatures
(list of parameters) of the constructors must be unique
▪ i.e., each constructor must have a unique list of parameter
types
37
37
/**
* Initializes the x and y coordinate of the point to
* the argument values.
*
* @param x the x coordinate of the point
* @param y the y coordinate of the point
*/
public Point2D (float x, float y) {
x = x;
y = y;
}
Compile?
Working?
Local variable shadows the
38 instance variable
38
19
2020-09-30
this (shadowing)
• there are parameters with the same names as fields
▪ when this occurs, the parameter (smaller scope) has
precedence over the field (larger scope)
▪ we say that the parameter shadows the field
public Point2D(float x, float y) { public Point2D(float x2, float y2) {
x = x; x = x2;
y = y; will compile y = y2;
but nothing change }
39
}
39
/**
* Initializes the x and y coordinate of the point to
* the argument values.
*
* @param x the x coordinate of the point
* @param y the y coordinate of the point
*/
public Point2D (float x, float y) {
this.x = x;
this.y = y;
} this.x : the field named x of this point
this.y : the field named y of this point
x : the parameter named x of the constructor
y : the parameter named y of the constructor
40
40
20
2020-09-30
41
41
Custom constructors
• adding the constructor Point2D(float x, float y) allows the
client to simplify their code
public static void main(String[] args) {
// create a point
Point2D p = new Point2D();
}
42
42
21
2020-09-30
}
43
43
44
44
22
2020-09-30
45
public Point2D () {
this.x = -1.0f;
this.y = 0.0f;
}
46
23
2020-09-30
Another example
47
3. Copy constructor
• a copy constructor initializes the state of an object by
copying the state of another object (having same type)
▪ it has a single parameter that is the same type as the class
/**
* Sets the x and y coordinate of this point by copying
* the x and y coordinate of another point.
*
* @param other a point to copy
*/
public Point2D (Point2D other) {
is ‘this’ this.x = other.x; other.x is okay inside
a must?
48 this.y = other.y; class, not okay outside
}
48
24
2020-09-30
Copy constructor
• adding a copy constructor allows the client to simplify
their code
49
49
Copy constructor
• adding a copy constructor allows the client to simplify
their code
50
50
25
2020-09-30
public Point2D() {
this.x = -1.0f; Is ‘this’ a
this.y = 0.0f; must?
}
51
52
52
26
2020-09-30
Constructor chaining
• a constructor is allowed to invoke another constructor
• when a constructor invokes another constructor it is
called constructor chaining
• to invoke a constructor in the same class you use the
this keyword
▪ if you do this then it must occur on the first line of the
constructor body
o but you cannot use this in a method to invoke a constructor
53
53
Constructor chaining
public class Point2D {
public float x;
public float y;
public Point2D() {
this.x = -1.0f;
this.y = 0.0f;
}
54
27
2020-09-30
Constructor chaining
public class Point2D {
public float x;
public float y;
public Point2D() {
this(-1.0f, 0.0f);
}
invokes
55
55
Constructor chaining
public class Point2D {
public float x;
public float y;
public Point2D() {
this(-1.0f, 0.0f);
…. // more code is needed
}
invokes
56
56
28
2020-09-30
▪ In instance method,
o Reference to context object, can resolve ambiguity between
instance variables and parameters in constructor this.x = x;
o to call another method from same class. this.move();
o to return current object to caller return this;
o to pass the current object as a parameter to another method
method(this);
57
57
58
58
29
2020-09-30
Encapsulation
• we can add features to Point2D to make it easier to use
▪ we can add access modifiers to fields to prevent unauthorized
access or manipulation of data (information hiding)
▪ we can (need to) add methods that use the fields of Point2D to
perform some sort of computation
o e.g., provide access to the values of the fields
o e.g., calculate distance between two points, or distance to origin
o e.g., translate, rotate or scale a point (w.r.t. an origin)
59
Recall:
Organization of a Typical Java Program
package 2030.lab0;
• one or more files
import java.util;
• zero or one package
import java.util; name
• zero or more import
Class Student {
statements
int age; • one class
public student()
• zero or more fields
{…} (attributes)
public int getAge() • zero or more constructors
} {…}
• zero or more methods
60
60
30
2020-09-30
Methods
Basics
Accessors and Mutators
Anonymous objects
Object aliasing and pass-by-value
61
61
Methods
• a method performs some sort of computation
• a method is reusable
▪ anyone who has access to the method can use the method without
copying the contents of the method
▪ anyone who has access to the method can use the method without
knowing the contents of the method
62
31
2020-09-30
Method header
63
63
• the parameter list is the list of types and names that appear
inside of the parentheses
parameter list
64
64
32
2020-09-30
65
Method signature
signature
66
66
33
2020-09-30
Method signature
67
67
Method signature
or
public int distanceTo(double r, double theta)
68
68
34
2020-09-30
69
69
Method signature
two or more methods with the same name but different
signatures are said to be overloaded
70
35
2020-09-30
add(1,3,5,7)
add(3,7)
add(3,5,7)
71
int[] a = {7,3,5,6,8,9};
Arrays.sort(a);
72
char[] c={'b','c','d','x','a'};
Arrays.sort(c);
72
36
2020-09-30
Constructor overloading
public class Point2D {
public float x;
public float y;
public Point2D() {
this.x = -1.0f;
this.y = 0.0f; Three overloaded
} constructors.
Must have different
public Point2D(float x, float y) { parameter list
this.x = x;
this.y = y;
}
73
74
74
37
2020-09-30
/**
* return the distance to coordinate x y.
*
* @param x, the x coordinate
* @param y the y coordinate
*/
75
package eecs2030.lab0;
another example
public class Lab0 {
}
we create a variable result to store the return value of the method
76
76
38
2020-09-30
77
package eecs2030.lab0;
78
39
2020-09-30
package eecs2030.lab0;
Alternative implementations
79 • there are many ways to implement this particular method
79
package eecs2030.lab0;
Alternative implementations
80 • there are many ways to implement this particular method
80
40
2020-09-30
package eecs2030.lab0;
} Operator precedence
Relational before Logical
<= >= before && ||
Assignment = is lower
Alternative implementations
81 • there are many ways to implement this particular method
81
package eecs2030.lab0;
Alternative implementations
82 • there are many ways to implement this particular method
82
41
2020-09-30
Methods
Basics
Accessors and Mutators
Anonymous objects
Object aliasing and pass-by-value
83
83
84
42
2020-09-30
/**
* Returns the x coordinate of this point.
*
* @return the x coordinate of this point
*/
public float getX() {
return this.x;
}
is ‘this’ a
must?
85
85
/**
* Returns the y coordinate of this point.
*
* @return the y coordinate of this point
*/
public float getY() {
return this.y;
}
is ‘this’ a
must?
86
86
43
2020-09-30
/** p.setX(2.2f);
* Sets the y coordinate to the specified value.
*
* @param newY the new y coordinate
*/
public void setY(float y) { is ‘this’ a
must?
this.y = y;
87
} p.setY(4.5f);
87
q.setX(100.0f);
q.setY(200.0f);
System.out.println("q = (" + q.getX() + ", " + q.getY() + ")");
}88
88
44
2020-09-30
89
89
90
90
45
2020-09-30
is ‘this’ a
must?
91
91
System.out.println(p.setY(7.0f)); // valid?
float b = p.setY(7.0f); // valid?
return p.setY(7.0f); // valid?
p.setX(6.5f).setY(7.0f);
92 // valid?
92
46
2020-09-30
/**
* move the x coordinate with the specified value.
*
* @param deltaX the moving amount
* @return a reference to this point
*/
public Point2D moveX2(float deltaX) {
this.x += deltaX;
return this; Allow a call chain in one line
94
} p.moveX2(4.5).moveY2(3.4);
94
47
2020-09-30
▪ In instance method,
o Reference to context object, can resolve ambiguity between
instance variables and parameters in constructor this.x = x;
o to call another method from same class. this.move();
o to return current object to caller return this;
o to pass the current object as a parameter to another method
method(this);
95
95
Class Diagram
UML Point2D
- x : int
• Public members are - y : int
shown by + + Point()
• Private members are + Point(int, int)
shown by - + getX(): int
• Protected members are + getX(): int
shown by # + setX(int): void
• Package members are + setY(int): void
shown by ~ + moveX(int): void
+ equals(Object) : Boolean
+ hashCode() : int
+ toString() : String
...
96
48
2020-09-30
• This week
▪ Classes
o basics
o encapsulation
o fields
o constructors
o methods
97
49