[go: up one dir, main page]

0% found this document useful (0 votes)
18 views43 pages

Chapter 2

Uploaded by

mjny470t
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)
18 views43 pages

Chapter 2

Uploaded by

mjny470t
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/ 43

Ahmad Khoj

King Abdulaziz University

KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬


KING ABDULAZIZ UNIVERSITY

Chapter 2
Object Oriented
programming concepts

KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬


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.


 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.
3
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Objects
Class Name: Person A class template

Data Fields:
name is _______

Methods:
getName

Person O1 Person O2 Person O3 Three objects of


the Person class
Data Fields: Data Fields: Data Fields:
name is Ahmad name is Zaki name is Rashad

 An object has both a state and behavior.


 The state defines the object.
 The behavior defines what the object does.
4
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Classes

 Classes are constructs that define objects of the


same type.
 A Java class uses:
 variables to define data fields.
 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.
5
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Classes
class Person {

String name ; Data field

Person() {
}
Constructors

Person(String n) {
name = n;
}

double getName() { Method


return name;
}
} 6
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
UML Class Diagram

UML Class Diagram Person Class name

name: String Data fields

Person() Constructors and


Person(n: String) Methods
getName(): String

O2: Person O3: Person UML notation


O1: Person
for objects
name: Ahmad name: Zaki name: Rashad

7
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
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:
Person o1;
8
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Constructors

 Constructors are a special kind of


methods that are invoked to construct
objects.

Person() {
}

Person(String n) {
name = n;
} KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
9
Constructors, cont.
 Constructors play the role of initializing objects.
 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.
10
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Creating Objects Using Constructors

new ClassName();

Example:
new Person();

new Person(“Ahmad Khoj”);

11
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
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.

12
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Declaring/Creating Objects in a Single Step

ClassName objectRefVar = new ClassName();

Assign object reference Create an object


Example:
Person O1 = new Person();

13
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Accessing Objects

 Referencing the object’s data:


objectRefVar.data
e.g. o1.name

 Invoking the object’s method:


objectRefVar.methodName(arguments)
e.g. o1.getName()

14
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Instance Variables, and Methods

 Instance variables belong to a specific instance.

 Instance methods are invoked by an instance of the class.

15
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Trace Code
Declare o1

Person o1 = new Person (“Ahmad”); no value


o1
Person o2 = new Person();

o2.name = “Zaki”;

16
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Code, cont.

Person o1 = new Person (“Ahmad”); no value


O1
Person o2 = new Person();

o2.name = “Zaki”; : Person

name: Ahmad

Create a new
Person object

17
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Code, cont.

Person o1 = new Person (“Ahmad”); reference value


O1
Person o2 = new Person();
Assign object reference
o2.name = “Zaki”; to o1 : Person

name: Ahmad

18
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Code, cont.


o1 reference value

Person o1 = new Person (“Ahmad”);


: Person
Person o2 = new Person();
name: Ahmad
o2.name = “Zaki”;

o2 no value

Declare o2

19
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Code, cont.


o1 reference value

Person o1 = new Person (“Ahmad”);


: Person
Person o2 = new Person();
name: Ahmad
o2.name = “Zaki”;

o2 no value

: Person
Create a new name: null
Person object

20
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Code, cont.


o1 reference value

Person o1 = new Person (“Ahmad”);


: Person
Person o2 = new Person();
name: Ahmad
o2.name = “Zaki”;

o2 reference value

Assign object reference


to o2 : Person

name: null

21
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Code, cont.


o1 reference value

Person o1 = new Person (“Ahmad”);


: Person
Person o2 = new Person();
name: Ahmad
o2.name = “Zaki”;

o2 reference value

: Person
Change name in name: Zaki
o2

22
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
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

23
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
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
24
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
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.

25
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
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.

26
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Visibility Modifiers and Accessor/Mutator Methods

 By default, the class, variable, or method can be


accessed by any class in the same package.
 public
The class, data, or method is visible to any class in any
package.

 private
The data or methods can be accessed only by the
declaring class.
 The get and set methods are used to read and modify
private properties.
27
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
package p1; package p2;
public class C1 { public class C2 { public class C3 {
public int x; void aMethod() { void aMethod() {
int y; C1 o = new C1(); C1 o = new C1();
private int z; can access o.x; can access o.x;
can access o.y; cannot access o.y;
public void m1() { cannot access o.z; cannot access o.z;
}
void m2() { can invoke o.m1(); can invoke o.m1();
} can invoke o.m2(); cannot invoke o.m2();
private void m3() { cannot invoke o.m3(); cannot invoke o.m3();
} } }
} } }

package p1; package p2;


class C1 { public class C2 { public class C3 {
... can access C1 cannot access C1;
} } can access C2;
}

 The private modifier restricts access to within a class.


 The default modifier restricts access to within a package.
 The public modifier enables unrestricted access.
28
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
NOTE

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

private int convert(boolean b) {


return x ? 1 : -1;
}
}
(a) This is OK because object foo is used inside the Foo class (b) This is wrong because x and convert are private in Foo.

29
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Why Data Fields Should Be private?

1. To protect data.

2. To make class easy to maintain.

30
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Static Variables, Constants, and Methods

 Static variables are shared by all the instances of


the class.
 Static constants are final variables shared by all
the instances of the class.
 Static methods are not tied to a specific object.
 To declare static variables, constants, and methods, use
the static modifier.

31
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Static Variables, Constants, and Methods

instantiate Memory
circle1

radius = 1 1 radius
Circle numberOfObjects = 2

radius: double
numberOfObjects: int
2 numberOfObjects
getNumberOfObjects(): int
+getArea(): double instantiate
circle2

radius = 5 5 radius
UML Notation: numberOfObjects = 2
underline: static variables or methods

32
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Static Variables, Constants, and Methods

33
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Scope of Variables

 The scope of a local variable starts from its


declaration and continues to the end of the
block that contains the variable.
 A local variable must be initialized explicitly before it
can be used.
 The scope of instance and static variables is
the entire class.
 They can be declared anywhere inside a class.

34
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
The this Keyword

 Use this to refer to an instance data field.


 Use this to invoke an overloaded
constructor of the same class.
 Use this to refer to the object that invokes
the instance method.

35
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Calling Overloaded Constructor
public class Circle {
private double radius;

public Circle(double radius) {


this.radius = radius;
} this must be explicitly used to reference the data
field radius of the object being constructed
public Circle() {
this(1.0);
} this is used to invoke another constructor

public double getArea() {


return this.radius * this.radius * Math.PI;
}
} Every instance variable belongs to an instance represented by this,
which is normally omitted
36
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Array of Objects

 An array of objects is actually an array of reference


variables.
 e.g. circleArray references to the entire array.
Circle[] circleArray = new Circle[10];

circleArray reference circleArray[0] Circle object 0


circleArray[1]

… Circle object 1

circleArray[9] Circle object 9

37
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Array of Objects, cont.

 circleArray[1] references to a Circle


object.

 So invoking circleArray[1].getArea()
involves two levels of referencing as shown
in the previous figure.

38
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Array of Objects Example

Output:
Ahmad
Zaki
Rashad
39
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
UML Example

40
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
UML Example, cont.

Output:
Ahmad
31
true

Ahmad Khoj
60
false

41
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Exercise
Build a java class called “Bill” contain a method “getTax” that calculate tax according to
the following formula:
Tax = invoice * 0.05
Create a suitable object to acquire the required result.

42
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Introduction to Java Programming
)10th Edition(

By
Y. Daniel Liang

KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬

You might also like