Lecture 5-2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Computer Programming-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..)

✓ 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.

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

Circle Object 1 Circle Object 2 Circle Object 3 Three objects of


the Circle class
Data Fields: Data Fields: Data Fields:
radius is 10 radius is 25 radius is 125

✓ An object has both a state and behavior.


✓ The state defines the object, and the behavior
defines what the object does.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
5
Classes
✓ Classes are constructs that define objects of the
same type.
✓ A Java class uses variables to define data fields
and methods to define behaviors.
✓ Additionally, a class provides a special type of
methods, known as constructors, which are
invoked to construct objects from the class.

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

/** Construct a circle object */


Circle() {
}
Constructors
/** Construct a circle object */
Circle(double newRadius) {
radius = newRadius;
}

/** Return the area of this circle */


double getArea() { Method
return radius * radius * 3.14159;
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
7
UML Class Diagram
UML Class Diagram Circle Class name

radius: double Data fields

Circle() Constructors and


Circle(newRadius: double) methods
getArea(): double

circle2: Circle circle3: Circle UML notation


circle1: Circle
for objects
radius = 1.0 radius = 25 radius = 125

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
8
Example: Defining Classes and
Creating Objects

Objective: Demonstrate creating objects,


accessing data, and using methods.

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

Objective: Demonstrate creating objects,


accessing data, and using methods.

TV

TestTV Run

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
10
Constructors

Circle() { Constructors are a special


} kind of methods that are
invoked to construct objects.
Circle(double newRadius) {
radius = newRadius;
}

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.

✓ To declare a reference variable, use the syntax:

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();

Assign object reference Create an object


Example:
Circle myCircle = new Circle();

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

✓ Invoking the object’s method:


objectRefVar.methodName(arguments)
e.g., myCircle.getArea()

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
17
animation
Trace Code
Declare myCircle

Circle myCircle = new Circle(5.0); no value


myCircle
SCircle yourCircle = new Circle();

yourCircle.radius = 100;

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
18
animation

Trace Code, cont.

Circle myCircle = new Circle(5.0); no value


myCircle
Circle yourCircle = new Circle();

yourCircle.radius = 100; : Circle

radius: 5.0

Create a circle

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
19
animation

Trace Code, cont.

Circle myCircle = new Circle(5.0);


myCircle reference value
Circle yourCircle = new Circle();

yourCircle.radius = 100; Assign object reference : Circle


to myCircle
radius: 5.0

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
20
animation

Trace Code, cont.


Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();

yourCircle.radius = 100; : Circle

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

Trace Code, cont.


Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();

yourCircle.radius = 100; : Circle

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

Trace Code, cont.


Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();

yourCircle.radius = 100; : Circle

radius: 5.0

yourCircle reference value

Assign object reference


to yourCircle : Circle

radius: 1.0

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
23
animation

Trace Code, cont.


Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();

yourCircle.radius = 100; : Circle

radius: 5.0

yourCircle reference value

: 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.

Can you invoke getArea() using Circle1.getArea()? The answer is no.


All the methods used before this chapter are static methods, which are
defined using the static keyword.
However, getArea() is non-static. It must be invoked from an object
using

objectRefVar.methodName(arguments) (e.g., myCircle.getArea()).


More explanations will be given in the section on “Static Variables,
Constants, and Methods.”

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);
}
}

Compilation error: variables not


initialized
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
29
Differences between Variables of
Primitive Data Types and Object Types
Created using new Circle()
Primitive type int i = 1 i 1

Object type Circle c c reference c: Circle

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

Object type assignment c1 = c2

Before: After:

c1 c1

c2 c2

c1: Circle C2: Circle c1: Circle C2: Circle


radius = 5 radius = 9 radius = 5 radius = 9

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

You might also like