Objects Classes
Objects Classes
1
OO Programming Concepts
2
OO Programming Concepts
3
Principles of OOP
Abstraction
Encapsulation
Inheritance
Polymorphism
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
27
Principles of OOP
Abstraction
Encapsulation
Inheritance
Polymorphism
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
27
Abstraction
Abstraction:
– represent complex real world in simplest manner.
– identify the relevant qualities and behaviors of an object
– represent the necessary feature and ignore the unnecessary
ones
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Abstraction
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Abstraction (Example)
A person in different roles:
– At school: "Student".
– At work: "Employee".
– At government institution: "Citizen".
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Classes
9
Classes
10
Objects
11
UML Class Diagram
12
Constructors
Constructors are a special kind of methods that
are invoked/called to construct objects.
Circle() {
}
Circle(double newRadius) {
radius = newRadius;
}
13
Constructors, cont.
Constructors:
with no parameters is referred to as a no-arg
constructor.
must have the same name as the class itself.
do not have a return type—not even void.
are invoked using the new operator when an
object is created.
play the role of initializing objects.
14
Creating Objects Using
Constructors
Format:
new ClassName();
Example:
new Circle();
new Circle(5.0);
15
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.
16
Declaring Object Reference Variables
To reference an object, assign the object to a reference
variable.
Format:
ClassName objectRefVar;
Example:
Circle myCircle;
17
Declaring/Creating Objects
in a Single Step
ClassName objectRefVar = new ClassName();
18
Accessing Objects
Referencing the object’s data:
objectRefVar.data
e.g., myCircle.radius
19
animation
Trace Code Declare, create,
and assign in one
statement
JFrame frame1 = new JFrame(); frame1
:
reference
JFrame
frame1.setTitle("Window 1"); titl
frame1.setSize(200, 150); e:
frame1.setVisible(true); JFrame wi
dth:
frame2 = new JFrame(); hei
frame2.setTitle("Window 2"); ght:
frame2.setSize(200, 150); visi
ble:
frame2.setVisible(true);
20
animation
Trace Code
JFrame frame1 = new JFrame(); frame1 reference
Set title property
frame1.setTitle("Window 1"); : JFrame
frame1.setSize(200, 150); title:
frame1.setVisible(true); JFrame "Window 1"
width:
frame2 = new JFrame(); height:
frame2.setTitle("Window 2"); visible:
frame2.setSize(200, 150);
frame2.setVisible(true);
21
animation
Trace Code
JFrame frame1 = new JFrame(); frame1 reference
frame1.setTitle("Window 1"); : JFrame
Set size property
frame1.setSize(200, 150); title:
frame1.setVisible(true); "Window 1"
width: 200
JFrame frame2 = new JFrame(); height: 150
frame2.setTitle("Window 2"); visible:
frame2.setSize(200, 150);
frame2.setVisible(true);
22
animation
Trace Code
JFrame frame1 = new JFrame(); frame1 reference
frame1.setTitle("Window 1"); : JFrame
frame1.setSize(200, 150); title:
frame1.setVisible(true); "Window 1" Set visible
width: 200 property
JFrame frame2 = new JFrame(); height: 150
frame2.setTitle("Window 2"); visible: true
frame2.setSize(200, 150);
frame2.setVisible(true);
23
animation
Trace Code
JFrame frame1 = new JFrame(); frame1 reference
frame1.setTitle("Window 1"); : JFrame
frame1.setSize(200, 150); title:
frame1.setVisible(true); "Window 1"
width: 200
JFrame frame2 = new JFrame(); height: 150
frame2.setTitle("Window 2"); visible: true
frame2.setSize(200, 150); Declare, create,
:
frame2 reference and assign in one
frame2.setVisible(true); JFrame
statement
titl
e:
wi
dth:
hei
ght:
visi
ble:
24
animation
Trace Code
JFrame frame1 = new JFrame(); frame1 reference
frame1.setTitle("Window 1"); : JFrame
frame1.setSize(200, 150); title:
frame1.setVisible(true); "Window 1"
width: 200
JFrame frame2 = new JFrame(); height: 150
frame2.setTitle("Window 2"); visible: true
frame2.setSize(200, 150);
frame2 reference
frame2.setVisible(true);
: JFrame Set title property
title:
"Window 2"
width:
height:
visible:
25
animation
Trace Code
JFrame frame1 = new JFrame(); frame1 reference
frame1.setTitle("Window 1"); : JFrame
frame1.setSize(200, 150); title:
frame1.setVisible(true); "Window 1"
width: 200
JFrame frame2 = new JFrame(); height: 150
frame2.setTitle("Window 2"); visible: true
frame2.setSize(200, 150);
frame2 reference
frame2.setVisible(true);
: JFrame
title:
Set size property
"Window 2"
width: 200
height: 150
visible:
26
animation
Trace Code
JFrame frame1 = new JFrame(); frame1 reference
frame1.setTitle("Window 1"); : JFrame
frame1.setSize(200, 150); title:
frame1.setVisible(true); "Window 1"
width: 200
JFrame frame2 = new JFrame(); height: 150
frame2.setTitle("Window 2"); visible: true
frame2.setSize(200, 150);
frame2 reference
frame2.setVisible(true);
: JFrame
title:
"Window 2" Set visible
width: 200 property
height: 150
visible: true
27
Principles of OOP
Abstraction
Encapsulation
Inheritance
Polymorphism
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
27
Encapsulation
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Encapsulation (Example)
• For example:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Encapsulation in Java
The variables of a class might need to be hidden from other
classes, and can be accessed only through the methods of their
current class, it is also known as data hiding.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Encapsulation in Java (Example)
public class EncapsulationTest{
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Encapsulation in Java (Example)
public class RunEncapsulation{
Output:
Name : James Age : 20
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Benefits of Encapsulation in Java
Benefits of Encapsulation:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2