Bahria University Karachi Campus
CSC- 210
Object Oriented Programming
Lecturer
Sameena Javaid
https://sites.google.com/site/sameenajavaidcs
Bahria University Karachi Campus
LECTURE 4
ENCAPSULATION
(CLASSES AND OBJECTS)
OUTLINE
• Class and Object (Encapsulation)
• UML
• Access modifiers
Bahria University Karachi Campus
CLASS VS OBJECT
• A class is a blue print of a particular classification of objects
• A class could be considered as a set of objects having the
same characteristics and behavior.
• The process of creating an object out of a template (class) is
called instantiation, “creating an instance of the class”, etc.
• An object is an instance of a class
• An object belongs to a class of objects
• Memory space
• Class: not allocated
• Object: allocated
• Declaration
• Class: once
• Object: many times
Bahria University Karachi Campus
Declaring a Class with Java
public class ClassName {
// Attributes
// Constructor(s)
// Methods (services)
}
Declaring Attributes With Java
<modifiers> <data type> <attribute name> ;
Modifiers Data Type Name
public String studentName ;
Bahria University Karachi Campus
OBJECT CREATION
A. The instance
variable is allocated crs
in memory.
Object: Course
B. The object is
A created
studentName
courseCode
Course crs; B
C. The reference of the
crs = new Course ( ); object created in B is crs
assigned to the variable.
Object: Course
C studentName
courseCode
Code State of Memory
Bahria University Karachi Campus
Assigning Objects’ References to the same
Instance Variable
crs
Course Course
A
A. The variable is
Course crs; B allocated in memory.
crs = new Course ( ); B. The reference to the
new object is assigned
crs = new Course ( ); to crs.
C. The reference to
another object overwrites
the reference in crs.
Code State of Memory
Bahria University Karachi Campus
Assigning an Object Reference From One
Variable to Another
crs1
crs2
A Course
Course crs1, crs2, B A. Variables are
allocated in memory.
crs1 = new Course( );
B. The reference to the new
crs2 = crs1;
object is assigned to crs1 .
C C. The reference in crs1 is
assigned to crs2.
Code State of Memory
Bahria University Karachi Campus
Assigning an Object Reference From One
Variable to Another
A. Variables are crs1
allocated in memory.
crs2
A. Variables are crs1
assigned references
A of objects.
Course
Course crs1, crs2, B crs2 Course
crs1 = new Course( );
crs2 = new Course( ); C. The reference in crs2 is crs1
assigned to crs1.
crs1 = crs2; Course
C crs2 Course
Bahria University Karachi Campus
Accessing Instance Attributes
• In order to access attributes of a given object:
• use the dot (.) operator with the object reference
(instance variable) to have access to attributes’ values
of a specific object.
.
instanceVariableName attributeName
course1.studentName= “Sara Kabir“;
course2.studentName= “Maha AlSaad“;
course2 course1
Object: Course Object: Course
studentName Maha AlSaad studentName Sara Kabir
courseCode courseCode
Bahria University Karachi Campus
class Course {
// Instance attributes
public String studentName;
public String courseCode ;
}
public class CourseRegistration {
public static void main(String[] args) {
Course course1, course2; CourseRegistration
//Create and assign values to course1
course1 = new Course( );
course1.courseCode= new String(“CSC112“); +main()
course1.studentName= new String(“Sara Kabir“);
//Create and assign values to course2
course2 = new Course( );
course2.courseCode= new String(“CSC107“);
course2.studentName= new String(“Maha AlSaad“);
System.out.println(course1.studentName + " has the course “+
course1.courseCode);
System.out.println(course2.studentName + " has the course “+
course2.courseCode);
}
}
Bahria University Karachi Campus
Instance vs. Primitive Variables
• Primitive variables hold values of primitive data
types.
• Instance variables hold references of objects:
the location (memory address) of objects in
memory.
• Note: Memory addresses are usually written in
hexadecimal notation, beginning with a 0x (for
example, 0x334009). These addresses are unique
for each object and are assigned while a
program runs.
Bahria University Karachi Campus
UML
The Unified Modeling Language (UML) is
a general-purpose, developmental,
modeling language in the field of
software engineering, that is intended to
provide a standard way to visualize the
design of a system.
Bahria University Karachi Campus
UML CLASS DIAGRAM
• Classes are the basic components of any object
oriented software system and UML class
diagrams provide an easy way to represent
these.
• A class diagram is at the heart of UML. It
represents the core purposes of UML because it
separates the design elements from the coding
of the system.
• UML was set up as a standardized model to
describe an object-oriented programming
approach.
Bahria University Karachi Campus
BASICS OF CLASS DIAGRAM
The class diagram is composed of three parts:
• Upper section - Name of the class - This section is
always required whether you are talking about
the classifier or an object.
• Middle Section - Attributes of the class - The
attributes describe the variables that describe
the qualities of the class. This is only required
when describing a specific instance of a class.
• Bottom section - Class operations (methods) -
Displayed in list format, each operation takes up
its own line. The operations describe how a class
can interact with data.
Bahria University Karachi Campus
CLASS DIAGRAM ANATOMY
Bahria University Karachi Campus
MEMBER ACCESS MODIFIER
All classes have different access levels depending
on the access modifier (visiblity). Here are the
following access levels with their corresponding
symbols:
•Public (+)
•Private (-)
•Protected (#)
•Package (~)
•Derived (/)
•Static (underlined)
Bahria University Karachi Campus
Examples
Bahria University Karachi Campus
MODIFIERS
Bahria University Karachi Campus
ACCESS SPECIFIER (MODIFIERS)
• A mechanism to set access levels for
classes, attributes, variables, methods and
constructors.
• The following are access levels:
• static
• public +
• private -
• protected #
• default
Bahria University Karachi Campus
STATIC
• It used to define class attributes and
methods.
• Class attributes (and methods):
• live in the class
• are shared by all objects of the class.
• do not belong to objects’ states.
<modifiers> <data type> <attribute name> ;
Modifiers Data Type Name
public static int studentNumber ;
Bahria University Karachi Campus
CLASS ATTRIBUTES ACCESS
• Class attributes (and methods) can also be
manipulated without creating an instance of
the class.
<class name>.<attribute name>
Class Name Attribute Name
Course.studentNumber = 0 ;
Bahria University Karachi Campus
CLASS ATTRIBUTES (SHARED MEMORY)
All objects share
variable count
student1
public class Student {
id
static int count =0; count = 0
firstName
lastName
String id;
dateOfBirth
String firstName;
String lastName;
String dateOfBirth;
}
student2
id
firstName
Both student1 and lastName
student2 have a copy dateOfBirth
of these variables
Bahria University Karachi Campus
public class Course {
// attributes
public String studentName;
public String courseCode ;
public static int studentNumber;
}
CourseRegistration
public class CourseRegistration {
public static void main(String[] args) {
Course course1, course2; +main()
//Create and assign values to course1
course1 = new Course( ); Course.studentNumber = 1;
course1.courseCode= new String(“CT1513“);
course1.studentName= new String(“Sara Kabir“);
//Create and assign values to course2
course2 = new Course( ); Course.studentNumber ++;
course2.courseCode= new String(“CSC107“);
course2.studentName= new String(“Maha AlSaad “);
System.out.println(course1.studentName + " has the course “+
course1.courseCode + “ ” +
Course.studentNumber);
System.out.println(course2.studentName + " has the course “+
course2.courseCode + “ ” +
Course.studentNumber);
}
}
Bahria University Karachi Campus
PUBLIC ACCESS
• If we declare any variable and method as
‘public’, it will be accessible to all the entire
class and visible to all the classes outside the
class. It can be accessed from outside
package also.
• For example,
public int val;
public void display();
Bahria University Karachi Campus
PUBLIC ACCESS
Package A Package
B
Super Class A Any Class B
public members
Any Class
Sub Class B
A
Sub Class
A
Bahria University Karachi Campus
public class Course {
// attributes
public String studentName;
public String courseCode ;
}
public class CourseRegistration { CourseRegistration
public static void main(String[] args) {
Course course1, course2; +main()
//Create and assign values to course1
course1 = new Course( );
course1.courseCode= “CT1513“;
course1.studentName= “Sara Kabir“;
//Create and assign values to course2
course2 = new Course( );
course2.courseCode= “CSC107“;
course2.studentName= “Maha AlSaad“;
System.out.println(course1.studentName + " has the course “+
course1.courseCode );
System.out.println(course2.studentName + " has the course “+
course2.courseCode );
}
}
Bahria University Karachi Campus
PROTECTED ACCESS
• A protected member is accessible in all classes
in the package containing its class and by all
subclasses of its class in any package where this
class is visible.
• In other words, non-subclasses in other
packages cannot access protected members
from other packages. Means, this visibility
control is strongly related to inheritance.
For example,
protected int val;
protected void display();
Bahria University Karachi Campus
PROTECTED ACCESS
Package A Package
B
Super Class A Any Class B
protected members
Any Class A
Sub Class B
Sub Class A
Bahria University Karachi Campus
DEFAULT ACCESS/ PACKAGE ACCESS
• The default access can also be called as package
access.
• When no member accessibility modifier is specified,
the member is only accessible inside its own package
only.
• Even if its class is visible in another package, the
member is not accessible there.
• Default member accessibility is more restrictive than
the protected member accessibility.
• For Example,
int val;
void display();
Bahria University Karachi Campus
DEFAULT ACCESS
Package A Package B
Super Class A Any Class B
Members with no
access modifier
given
Sub Class B
Any Class A
Sub Class A
Bahria University Karachi Campus
PRIVATE ACCESS
• This is most restrictive than all other visibility
controls.
• Private members are not accessible from any
other class.
• They can not be accessed even from any
subclass or any class from the same package. In
order to declare a member as private, we just
need to write ‘private’ in front of the member as,
private int val;
private void display();
Bahria University Karachi Campus
PRIVATE ACCESS
Package A Package B
Super Class A Any Class B
private members
Sub Class B
Any Class A
Sub Class A
Bahria University Karachi Campus
DECLARING PRIVATE ATTRIBUTES
<modifiers> <data type> <attribute name> ;
Modifiers Data Type Name
private String studentName ;
Bahria University Karachi Campus
EXAMPLE OF A CLASS WITH
PRIVATE ATTRIBUTES
Course
- studentName: String
- courseCode: String
public class Course {
// Attributes
private String studentName;
private String courseCode ;
// No method Members
}
Bahria University Karachi Campus
class Course {
// Data Member
private String studentName;
private String courseCode ;
}
public class CourseRegistration {
public static void main(String[] args) {
Course course1, course2;
//Create and assign values to course1
course1 = new Course( );
course1.courseCode= “CT1513“;
course1.studentName= “Sara Kabir“;
//Create and assign values to course2
course2 = new Course( );
course2.courseCode= “CSC107“;
course2.studentName= “Maha AlSaad“;
System.out.println(course1.studentName + " has the course “+
course1.courseCode);
System.out.println(course2.studentName + " has the course “+
course2.courseCode); }}
Bahria University Karachi Campus
ACCESSIBILITY EXAMPLE
… class Service {
public int
Service obj = new Service();
memberOne;
private int
obj.memberOne = 10; memberTwo;
public void
obj.memberTwo = 20; doOne() {
…
obj.doOne();
}
private void
obj.doTwo(); doTwo() {
… …
}
Client } Service
Bahria University Karachi Campus