0% found this document useful (0 votes)
41 views17 pages

Post - EECS2030A-L5-ClassesMethodsObjectsX

The document discusses key concepts about classes in object-oriented programming, including that a class bundles data and methods together, serves as a blueprint for creating objects which have a unique state, and contains both static and non-static features. Static features imply a lack of instantiation, with single versions in memory created at compile time, while non-static features imply instantiation of multiple object versions in memory created at run-time. Classes can also serve as utility classes containing only static constants and methods.

Uploaded by

Ya Lan
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)
41 views17 pages

Post - EECS2030A-L5-ClassesMethodsObjectsX

The document discusses key concepts about classes in object-oriented programming, including that a class bundles data and methods together, serves as a blueprint for creating objects which have a unique state, and contains both static and non-static features. Static features imply a lack of instantiation, with single versions in memory created at compile time, while non-static features imply instantiation of multiple object versions in memory created at run-time. Classes can also serve as utility classes containing only static constants and methods.

Uploaded by

Ya Lan
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/ 17

2020-09-25

Advanced Object Oriented


Programming

EECS 2030
F 2020 :: Section C

Advanced Object Oriented


Programming

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)

▪ A class is an implementation of a reference type


o Student, Point, …

Classes: implementation of reference type


• Most classes can serve as a blueprint for creating objects
▪ An object is an instantiation / instance of a class
▪ Instantiating objects implies the use of NON-STATIC
features
▪ Objects can each have a unique state
o state: set of current values of all of its non-static fields

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

Point2D p = new Point2D(1,2); // point ( 1, 2) 5


13 r
Point2D q = new Point2D(-3,8); // point (-3, 8)
Point2D r = new Point2D(5,13); // point ( 5,13)
6 }

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

100 Point2D class Point2D object


800
x Point2D(5,13) x 5
y y 13

p = null; // object at 600 unreachable, will be garbage collected (later)


‘reference count’ is 0

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

100 Point2D class Point2D object


800
x Point2D(5,13) x 5
y y 13

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

Classes have static and non-static features

• Static: implies lack of instantiation


▪ single versions (fields/methods/etc.) in memory
▪ created at compile time
▪ different usage from non-static. Visited later

• Non-Static: implies instantiation of objects


▪ multiple versions (fields/methods/etc.) in memory
▪ created at run-time q

r
9

Classes have static and non-static features


• A Class can be used bundle related data and methods
▪ E.g. java.lang.Math bundles constants like PI and common math
functions like sin(), cos(), and pow(), etc.
▪ Classes such as java.lang.Math have purely STATIC features
o Called utility class
o Don’t need to (actually cannot) construct
PI pow()
Math …

public static void main(String[] args) {

double radius = 10.5;


double area = Math.PI * Math.pow(radius,2); Visit later
System.out.println("Area = " + area);

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

• fields: "attributes", "instance variables"


color

▪ Data, state of object

Example: Point2D Class

• consider implementing our own class that represents 2-


dimensional points on a real cartesian plane (Point2D)
▪ a possible implementation would have:
o a field to represent the x-coordinate of the point as a float
o a field to represent the y-coordinate of the point as a float

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;

public class: any client can use


public class Point2D{ this class
public float x;
public float y; public fields: any client
can use these fields directly by name
}

Can we use the class (create object) now?


13 (no constructor, no methods)

13

Using Point2D

• even in its current form, we can use Point2D to create


and manipulate Point2D objects
public static void main(String[] args) {
// create a point
Point2D p = new Point2D();

// set its coordinates


p.x = -1.0f;
p.y = 1.5f;

// get its coordinates


System.out.println("p = (" + p.x + ", " + p.y + ")");
} p = (-1.0, 1.5)

14 System.out.println(p); eecs.2030.Point2D@15db9742

14

7
2020-09-25

Using Point2D

Immediate issues with current implementation:

1. initializing the coordinates of the point is inconvenient


▪ clients have to manually set the x and y coordinates

2. manipulating a point is somewhat inconvenient


▪ clients have to manually compute a string representation of the point
▪ clients have to manually compute the distance of two points

3. comparing two points using equals() may result in


unexpected results
▪ Point p1 = new Point(1,2); Point p2=new Point(1,2);
▪ p1.equals(p2); ?

4. poor Information hiding (later)


15 ▪ Data accessible to the public

15

public static void main(String[] args) {


// create a point
Point2D p = new Point2D();

// set its coordinates


p.x = -1.0f;
p.y = 1.5f;

// get its coordinates


System.out.println("p = (" + p.x + ", " + p.y + ")");

Point2D q = new Point2D();


q.x = 2.3f;
q.y = 4.5f;
System.out.println("q = (" + q.x + ", " + q.y + ")");

double d =Math.sqrt ((p.x-q.x)* (p.x-q.x)+ (p.y-q.y)*(p.y-q.y));

// 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 add constructors that set the values of the fields of a


Point2D object when it is created

▪ 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)

▪ we are obliged to define certain standard methods like


toStrings(), equals() so that we can display, test and compare
objects in a meaningful way

17

17

Encapsulation

This bundling of data and methods that use


the data into a single unit is known as
ENCAPSULATION

Typically achieved by:


restricting/controlling
access to internal data

allowing interaction through


constructors and methods
(expose only what a client needs)
()

color

18

18

9
2020-09-25

Access Modifiers

public, private, default, protected

19

19

Top level classes


• a top level class is a class that is not nested inside of another class
▪ Point2D is a top level class

• a top level class can have either:


▪ the public access modifier public Class Point2D{…}
o the class is visible to all other classes everywhere

▪ no access modifier default Class Point2D{…}


o called package access;
o the class is visible only inside its own package (and not its
sub-packages) import not help

• cannot be private, protected

20

20

10
2020-09-25

Public access, visible outside package (after import)

21

21

Default (package) access, not visible outside package (even after import)

22

22

11
2020-09-25

Access modifiers on fields (and methods)


Public: The access level is everywhere -- from within the class,
outside the class, within the package and outside the package.

Private: The access level is only within the class -- cannot be accessed
from outside the class..

Default (no modifier): The access level of a default modifier is only


within the package. --- cannot be accessed from outside the
package.

Protected: The access level is within the package + subclass


outside the package.

Class access modifier


take precedence!
• public field in default
class not accessible
outside package
23

23

Access modifiers on fields


• the safe rules of thumb:
▪ a field that represents a constant value can be public
o e.g., PI in java.lang.Math

▪ all other fields should be private

• protected fields appear when using inheritance (later)


24

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;

public class Point2D{


public class: any client can use this class
private float x;
private float y; private fields: visible only to this class
} .x .y okay only in the class

27

27

// in some other application

package eecs2030.basics;

public class PointMaker {

public static void main(String[] args) {

Point2D p = new Point2D();


no longer possible

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)

▪ we can add constructors that set the values of the fields of a


Point2D object when it is created

▪ we can 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)

▪ we are obliged to define certain standard methods (like


toStrings(), equals() so that we can display, test and compare
objects in a meaningful way

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

• a constructor: public Point2D(…)


▪ must have the same name as the class
▪ never returns a value (not even void)
o constructors are not methods
▪ can have zero or more parameters

• 3 fundamental types of constructor


▪ No-argument (default) constructor
▪ Custom constructor
33 ▪ Copy constructor

33

17

You might also like