0% found this document useful (0 votes)
2 views21 pages

Intro 15

The document discusses Object Oriented Programming concepts, focusing on encapsulation, which involves packaging variables and methods to protect data fields by declaring them private and providing public getter and setter methods. It also covers the benefits of encapsulation, UML diagrams for class structures, and the creation of immutable objects that cannot be modified after instantiation. Additionally, examples of encapsulation and the use of arrays of objects are provided to illustrate these concepts.

Uploaded by

jijjn7045
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)
2 views21 pages

Intro 15

The document discusses Object Oriented Programming concepts, focusing on encapsulation, which involves packaging variables and methods to protect data fields by declaring them private and providing public getter and setter methods. It also covers the benefits of encapsulation, UML diagrams for class structures, and the creation of immutable objects that cannot be modified after instantiation. Additionally, examples of encapsulation and the use of arrays of objects are provided to illustrate these concepts.

Uploaded by

jijjn7045
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/ 21

Lecture 10 : Object Oriented Programming

Part 3

Md. Shahriar Hussain


ECE Department, NSU

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
Encapsulation

• Packaging variables and methods into a single unit

• To prevent direct modifications of data fields, we should declare the data


fields private, using the private modifier. This is known as data field
encapsulation

• The meaning of Encapsulation, is to make sure that "sensitive" data is


hidden from users.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
2
Encapsulation

How to do data field encapsulation

• Declare class variables as private


• Provide public get and set methods to access and update the value of a
private variable

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
3
Encapsulation

• A private data field cannot be accessed by an object from outside the class
that defines the private field.
• However, a client often needs to retrieve and modify a data field.
• To make a private data field accessible, provide a getter method to return its
value.
• To enable a private data field to be updated, provide a setter method to set a
new value.
• A getter method is also referred to as an accessor and a setter to a mutator.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
4
Example
package Encapsulation;
public class Student {
private String name; // private = restricted access
}

package Encapsulation;
public class Test {
public static void main(String[] args) {
Student s = new Student();
s.name = "Rahim"; // error
System.out.println(s.name); // error
}
}

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
5
Encapsulation
package Encapsulation;
public class Student {
private String name; // private = package Encapsulation;
restricted access public class Test {
public static void main(String[] args) {
// Getter Student s = new Student();
public String getName() { s.setName("Rahim"); // Set the value
return name; of the name variable to “Rahim"
} System.out.println(s.getName());
}
}
// Setter
public void setName(String newName) {
this.name = newName;
}
}

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
6
Encapsulation

• Benefits of Encapsulation
– Better control of class attributes and methods
– Provides data hiding
– Class attributes can be made read-only ( by only using get method)
– Class attributes can be made write-only ( by only using set method)
– Increased security of data

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
7
Unified Modeling Language (UML)

• Unified modeling language (UML) diagrams describe the structure of a


system, the objects within the system, and how they all behave

• There are many different types of UML diagrams and each has a slightly
different symbol set

• Class diagrams are perhaps one of the most common UML diagrams used
and class diagram symbols center around defining attributes of a class

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
8
UML Class Diagram

In the class diagram

• the data field is denoted as


dataFieldName: dataFieldType

• The constructor is denoted as


ClassName(parameterName: parameterType)

• The method is denoted as


methodName(parameterName: parameterType): returnType

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
9
UML Class Diagram

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
10
UML Marker

Modifier Marker

Public +

Private -

Protected #

Static underline

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
11
UML and Encapsulation

• Create the Circle class that encapsulates circle properties and provides getter/setter and
other methods
• Create a client class name CircleTest that uses the Circle class to create a Circle object and
modifies the radius (make 5) using the setRadius method. Print the area.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
12
UML and Encapsulation
package Encapsulation;
public class Circle {
package Encapsulation;
private double radius=1; // private = restricted access
public class CircleTest {
private static int numberOfObject=0;
public static void main(String[] args) {
Circle(){
Circle c = new Circle();
numberOfObject++; c.setRadius(5.0);
} System.out.println("Area:" + c.getArea());
Circle(double radius){ }
this.radius=radius; }
numberOfObject++;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius=radius;
}
public static int getNumberOfObjects(){
return numberOfObject;
}
public double getArea(){
return radius * radius * Math.PI;
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
13
Array of Objects

• An array can hold objects as well as primitive type values.


• An array of objects is actually an array of reference variables.

Circle[] circleArray = new Circle[10];

for (int i = 0; i < circleArray.length; i++) {


circleArray[i] = new Circle();
}

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
14
Array of Objects

Circle[] circleArray = new Circle[10];

circleArray reference circleArray[0] Circle object 0


circleArray[1]

… Circle object 1

circleArray[9] Circle object 9

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
15
Array of Objects Example

• Create a client class name ArrayCircleTest


• Create a method name createCirle(). Create 5 circle object with random
radius (from 0 to 100) using that method.
• Create a method name printCircle(). Using this method, print all the circles
radius and area.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
16
Array of Objects Example
package Encapsulation;
public class CircleArrayTest {
public static void main(String[] args) {
Circle[] circleArray;
circleArray=createCircle();
printCircle(circleArray);
}
public static Circle[] createCircle(){
Circle[] circleArray= new Circle[5];
for(int i=0;i<circleArray.length;i++)
circleArray[i]=new Circle(Math.random()*100);
return circleArray;
}
public static void printCircle(Circle[] circleArray){
for(int i=0;i<circleArray.length;i++){
System.out.println("Radius:"+circleArray[i].getRadius()+" Area:"
+ circleArray[i].getArea());
}
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
17
Immutable Objects and Classes
• Normally, we create an object and allow its contents to be changed later.

• However, occasionally it is desirable to create an object whose contents cannot be


changed once the object has been created.

• We call such an object as immutable object and its class as immutable


class.

• The contents of immutable objects cannot be changed .

• If a class is immutable, then all its data fields must be private and it cannot contain
public setter methods for any data fields.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
18
Immutable Objects and Classes
package Encapsulation;
public class Circle {
private double radius; package Encapsulation;
Circle(double radius){ public class CircleTest {
this.radius=radius; public static void main(String[] args) {
Circle c = new Circle(5.0);
}
System.out.println("Area:" + c.getArea());
public double getRadius() { }
return radius; }
}
public double getArea(){
return radius * radius *Math.PI;
}
}

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
19
Immutable or not?
package Immutable; package Immutable;
public class Student { public class BirthDate {
private int id; private int year;
private int month;
private BirthDate birthDate; private int day;
Student(int ssn,int year, int month, int day){ BirthDate(int newYear,int newMonth, int newDay){
id = ssn; year = newYear;
birthDate = new BirthDate(year, month, day); month = newMonth;
} day = newDay;
}
public int getId() { public void setYear(int newYear){
return id; year = newYear;
} }
public BirthDate getBirthDate(){ public int getYear(){
return birthDate; return year;
}
} }
}
package Immutable;
public class Test {
public static void main(String[] args) {
Student student = new Student(201011, 1970, 5, 3);
BirthDate date = student.getBirthDate();
System.out.println(date.getYear());
date.setYear(2010); // Now the student birth year is changed!
System.out.println(date.getYear());
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
20
Immutable Objects and Classes

• A class with all private data fields and without mutators is not necessarily
immutable.
• For a class to be immutable, it must meet the following requirements
– All data fields must be private
– There can’t be any mutator methods/ setter methods for data fields
– No accessor methods can return a reference to a data field that is mutable

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
21

You might also like