Post - EECS2030A-L5-ClassesMethodsObjectsX
Post - EECS2030A-L5-ClassesMethodsObjectsX
EECS 2030
F 2020 :: Section C
EECS 2030
Lecture 5 :: CLASS BASICS
1
2020-09-25
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
}
{…}
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-25
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-25
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
Point2D w = p;
p = null; // object at 600 not unreachable, will not be garbage collected
‘reference count’ is not 0, as w reference it
8
4
2020-09-25
r
9
10 }
10
5
2020-09-25
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
} {…}
11
11
Implementing classes ()
() color
12
6
2020-09-25
/**
* 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;
13
Using Point2D
14 System.out.println(p); eecs.2030.Point2D@15db9742
14
7
2020-09-25
Using Point2D
15
// equals?
System.out.println("p.equals(q) is: " + p.equals(q));
} 16 equals() compile?
result?
16
8
2020-09-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)
▪ 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)
17
17
Encapsulation
color
18
18
9
2020-09-25
Access Modifiers
19
19
20
20
10
2020-09-25
21
21
Default (package) access, not visible outside package (even after import)
22
22
11
2020-09-25
Private: The access level is only within the class -- cannot be accessed
from outside the class..
23
24
12
2020-09-25
25
25
Other example
26
26
13
2020-09-25
/**
* A better way to implement fields of Point2D
*/
package eecs2030.basics;
27
27
package eecs2030.basics;
p.x = -1.0f;
p.y = 1.5f;
System.out.println("p = (" + p.x + ", " + p.y + ")");
28
28
14
2020-09-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)
29
29
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
{…}
30
30
15
2020-09-25
Constructors
31
31
32
32
16
2020-09-25
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
33
17