Lecture 5-2
Lecture 5-2
Lecture 5-2
Lecture 5
Dr.Sara A.shehab
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
1
Objects and Classes
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
OO Programming Concepts
✓ 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
behaviors.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
3
OO Programming Concepts (Cont..)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Objects
Class Name: Circle A class template
Data Fields:
radius is _______
Methods:
getArea
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
6
Classes
class Circle {
/** The radius of this circle */
double radius = 1.0; Data field
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
8
Example: Defining Classes and
Creating Objects
TestCircle1 Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
9
Example: Defining Classes and
Creating Objects
TV
TestTV Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
10
Constructors
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
11
Constructors, cont.
✓ A constructor with no parameters is referred to
as a no-arg constructor.
✓ Constructors must have the same name as the
class itself.
✓ Constructors do not have a return type—not
even void.
✓ Constructors are invoked using the new
operator when an object is created. Constructors
play the role of initializing objects.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
12
Creating Objects Using Constructors
new ClassName();
Example:
new Circle();
new Circle(5.0);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
13
Default Constructor
✓ A class may be declared without constructors.
✓ In this case, a no-arg constructor with an empty
body is implicitly declared in the class.
✓ This constructor, called a default constructor, is
provided automatically only if no constructors
are explicitly declared in the class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
14
Declaring Object Reference Variables
✓ To reference an object, assign the object to a
reference variable.
ClassName objectRefVar;
✓ Example:
Circle myCircle;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
15
Declaring/Creating Objects
in a Single Step
ClassName objectRefVar = new ClassName();
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
16
Accessing Objects
✓ Referencing the object’s data:
objectRefVar.data
e.g., myCircle.radius
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
17
animation
Trace Code
Declare myCircle
yourCircle.radius = 100;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
18
animation
radius: 5.0
Create a circle
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
19
animation
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
20
animation
radius: 5.0
yourCircle no value
Declare yourCircle
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
21
animation
radius: 5.0
yourCircle no value
: Circle
Create a new radius: 0.0
Circle object
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
22
animation
radius: 5.0
radius: 1.0
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
23
animation
radius: 5.0
: Circle
Change radius in radius: 100.0
yourCircle
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
24
Caution
Recall that you use
Math.methodName(arguments) (e.g., Math.pow(3, 2.5)) to invoke a
method in the Math class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
25
Reference Data Fields
✓ The data fields can be of reference types.
✓ For example, the following Student class contains a
data field name of the String type.
public class Student {
String name; // name has default value null
int age; // age has default value 0
boolean isScienceMajor; // isScienceMajor has default value false
char gender; // c has default value '\u0000'
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
26
The null Value
If a data field of a reference type does not
reference any object, the data field holds a
special literal value, null.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
27
Default Value for a Data Field
✓ The default value of a data field is null for a
reference type, 0 for a numeric type, false for a
Boolean type, and '\u0000' for a char type.
✓ However, Java assigns no default value to a local
variable inside a method.
public class Test {
public static void main(String[] args) {
Student student = new Student();
System.out.println("name? " + student.name);
System.out.println("age? " + student.age);
System.out.println("isScienceMajor? " + student.isScienceMajor);
System.out.println("gender? " + student.gender);
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
28
Example
Java assigns no default value to a local variable
inside a method.
public class Test {
public static void main(String[] args) {
int x; // x has no default value
String y; // y has no default value
System.out.println("x is " + x);
System.out.println("y is " + y);
}
}
radius = 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
30
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
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
31
Garbage Collection
✓ As shown in the previous figure, after the
assignment statement c1 = c2, c1 points to
the same object referenced by c2.
✓ The object previously referenced by c1 is no
longer referenced. This object is known as
garbage.
✓ Garbage is automatically collected by JVM.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
32
Garbage Collection, cont
✓ TIP: If you know that an object is no longer
needed, you can explicitly assign null to a
reference variable for the object.
✓ The JVM will automatically collect the space
if the object is not referenced by any variable.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
33
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807