Lecture 1
Lecture 1
Lecture 1
Programming II
1 – Objects and Classes(1)
1
1– Objects and Classes - Outline
Constructor
2
Object-Oriented Programming
(OOP)
➢ Procedural programming is about writing a function that
perform some calculations on the data.
➢ Object-oriented programming (OOP) is about creating
objects that contain related data and methods.
3
Object-Oriented Programming
(OOP)
Procedural programming
1. double x = 0.5;
2. double y = f(x);
3. double z = g(y);
4
Object-Oriented Programming
(OOP)
➢ Object-oriented programming (OOP) involves
programming using objects.
➢ An object represents an entity in the real world that can
be distinctly identified. For example, a student, a desk,
a circle, a button, and even a loan can all be viewed as
objects.
➢ An object has a unique identity, state, and behavior.
➢ The state of an object consists of a set of data fields
(also known as properties) with their current values.
➢ The behavior of an object is defined by a set of methods.
5
➢ Objects group together
➢ Primitives (int, double, char, etc...)
➢ Objects (String, etc...)
Customer
Object- String name
Oriented int id
6
Object-Oriented Programming
(OOP)
➢ Why use classes?
7
Object-Oriented Programming
(OOP)
➢ Object
➢ An object has a state and behavior.
➢ The state defines what the object is. Each object has its own state.
➢ The behavior defines what the object does. It is defined in the Class.
Data Fields:
Name is
Methods:
checkPayment()
9
Objects
Reference of an object
10
Objects
➢ In an object:
➢ Common variables are shared among certain operations.
➢ The variables are global only to these operations, but
not to others.
➢ The shared variables can be packaged into a common context
for the related operations.
11
Classes
12
Classes
class Circle {
/** The radius of this circle */
double radius = 1.0; Data field
TestCircle1 Run
15
Example: Defining Classes
and Creating Objects
➢ Objective:
Demonstrate
creating objects, accessing
data, and using methods.
TV
TestTV Run
16
Class Circle {
private double x, y;
private double radius;
public Circle(double newRadius) {
radius = newRadius;
}
A Class public double getArea() {
return Math.PI*radius*radius;
Definition }
public boolean contains(double x, double y)
{
double dx = x – this.x;
double dy = y - this.y;
return dx*dx + dy*dy <= radius*radius;
}
}
17
Constructors
18
Constructors
19
Default Constructors
Example:
Circle() {
}
20
Creating Objects Using
Constructors
new ClassName(); //creates a new instance and returns its reference
➢ Example:
new Circle();
new Circle(5.0);
21
Declaring Object Reference
Variables
➢ ClassName objectRefVar;
➢ Example:
Circle myCircle;
22
Declaring/Creating Objects
in a Single Step
➢ ClassName objectRefVar = new ClassName();
23
Accessing Objects
objectRefVar.data
e.g., myCircle.radius
objectRefVar.methodName(arguments)
e.g., myCircle.getArea()
24
Trace Code
Declare myCircle
yourCircle.radius = 100;
25
Trace Code, cont.
yourCircle.radius = 100;
: Circle
radius: 5.0
Create a circle
26
Trace Code, cont.
radius: 5.0
27
Trace Code, cont.
Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();
radius: 5.0
yourCircle no value
Declare yourCircle
28
Trace Code, cont.
Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();
radius: 5.0
yourCircle no value
: Circle
Create a new radius: 0.0
Circle object
29
Trace Code, cont.
Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();
radius: 5.0
radius: 0.0
30
Trace Code, cont.
Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();
radius: 5.0
: Circle
Change radius in radius: 100.0
yourCircle
31
More on Constructors and the
“this” Instance
➢ Constructor, which has exactly the same name as the defining
class, can be overloaded.
➢ One constructor can invoke other constructors of the class using
“this” before any other statements.
Class Circle {
public Circle(double x, double y, double radius)
{
this.x = x; this.y = y; this.radius=radius;
}
public Circle(double radius)
{ this(0.0, 0.0, radius)}
…
}
32
More on Constructors and the
“this” Instance
➢ Within a method, “this” refers to the instance
which the method is operating on.
➢ Within a method, if a name is not declared as a local variable or
a parameter, it is prefixed with “this.” by default.
Class Circle {
public Circle(double x, double y, double radius)
{
this.x = x; this.y = y; this.radius=radius;
}
public Circle(double radius)
{ this(0.0, 0.0, radius)}
…
}
33
More on Constructors and the
“this” Instance
➢ A local variable or a parameter hides the field with the same
name, to access the field, “this.” must be specified explicitly.
➢ When invoking new Circle(0.0,0.0,3.0),
this.radius in the constructor refers to the field radius of the
newly created instance.
Class Circle {
public Circle(double x, double y, double radius)
{
this.x = x; this.y = y; this.radius=radius;
}
public Circle(double radius)
{ this(0.0, 0.0, radius)}
…
}
34
Calling Overloaded
Constructor
public class Circle {
private double radius;
36
The null Value
37
Default Value for a Data Field
radius = 1
40
Copying Variables of Primitive
Data Types and Object Types
Primitive type assignment i = j
Before: After:
i 1 i 2
j 2 j 2
Before: After:
c1 c1
c2 c2
42
The Date Class
java.util.Date
The + sign indicates
public modifer +Date() Constructs a Date object for the current time.
+Date(elapseTime: long) Constructs a Date object for a given time in
milliseconds elapsed since January 1, 1970, GMT.
+toString(): String Returns a string representing the date and time.
+getTime(): long Returns the number of milliseconds since January 1,
1970, GMT.
+setTime(elapseTime: long): void Sets a new elapse time in the object.
43
The Date Class Example
44
Visibility Modifiers and
Accessor/Mutator Methods
➢ By default, the class, variable, or method can be accessed
by any class in the same package.
➢ Public
45
Visibility Modifiers and
Accessor/Mutator Methods
➢ The private modifier restricts access to within a class, the default modifier
restricts access to within a package, and the public modifier enables
unrestricted access.
46
Visibility Modifiers and
Accessor/Mutator Methods
➢ The private modifier restricts access to within a class, the default modifier
restricts access to within a package, and the public modifier enables
unrestricted access.
47
Visibility Modifiers and
Accessor/Mutator Methods
➢ An object cannot access its private members, as shown
in (b).
➢ It is OK, however, if the object is declared in its own class,
as shown in (a).
public class Foo { public class Test {
private boolean x; public static void main(String[] args) {
Foo foo = new Foo();
public static void main(String[] args) { System.out.println(foo.x);
Foo foo = new Foo(); System.out.println(foo.convert(foo.x));
System.out.println(foo.x); }
System.out.println(foo.convert()); }
}
48
To prevent direct modifications of fields,
the fields should be declared private. This
is known as data field encapsulation
To protect data.
Data Field
Encapsulation:
To make class easy to maintain.
49
Example of
Data Field Encapsulation
Circle
The - sign indicates
private modifier -radius: double The radius of this circle (default: 1.0).
-numberOfObjects: int The number of circle objects created.
50
Passing Objects to Methods
➢ Passing by value for primitive type value (the value is passed to
the parameter)
➢ Passing by value for reference type value (the value is the
reference to the object)
Stack Pass by value (here
the value is 5)
Space required for the
printAreas method Pass by value
int times: 5 (here the value is
Circle c: reference the reference for
the object) Heap
Space required for the
main method
int n: 5 A circle
myCircle: reference object
TestPassObject Run
51
➢ Variables are introduced by typings: T x .
➢ Instance variables: the typings are in class definitions.
class C { T x; }
52
Assignment 1
(Due date 2nd Sept.)
Define the Class of a kind of objects in our daily life (For example:
cars, chairs, etc.) with a constructor, 3~5 Data and 1~2 Methods.
Submit a document file or a text file of the Java code, and the UML
diagram of the Class.
Please submit your file with a filename as
“YourName_YourStudentId_Assign1”.
For example: WongUnHong_P1234567_Assign1.txt
In your Java code, all the Data should be initialized within the
constructor. However, the implementations of the Methods are not
necessary.
Return type should be void if nothing will be returned from a
Method.
54
Assignment 1
(Due date 2nd Sept.)
For Example: WongUnHong_P1234567_Assign1.txt
--------------------------------------------------
Circle
--------------------------------------------------
x : double
y : double
radius : double
--------------------------------------------------
Circle(newRadius : double)
getAera() : double
contains(x : double, y : double) : boolean
--------------------------------------------------
public class Circle {
private double x, y;
private double radius;
public Circle(double newRadius) {
x = 0;
y = 0;
radius = newRadius;
}
public double getArea() {}
public boolean contains(double x, double y) {}
}
55