Intro 15
Intro 15
Part 3
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
North South University CSE215 Md. Shahriar Hussain rights reserved. 0132130807
Encapsulation
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
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)
• 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
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
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 object 1
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
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.
• 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