Beruflich Dokumente
Kultur Dokumente
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 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
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
crs
Course Course
A
A. The variable is
Course crs; B allocated in memory.
C. The reference to
another object overwrites
the reference in crs.
crs1
crs2
A Course
Course
Course crs1, crs2, B crs2 Course
courseCode courseCode
Bahria University Karachi Campus
class Course {
// Instance attributes
public String studentName;
public String courseCode ;
}
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
•Public (+)
•Private (-)
•Protected (#)
•Package (~)
•Derived (/)
•Static (underlined)
Bahria University Karachi Campus
Examples
Bahria University Karachi Campus
MODIFIERS
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.
Course.studentNumber = 0 ;
Bahria University Karachi Campus
}
student2
id
firstName
Both student1 and lastName
student2 have a copy dateOfBirth
of these variables
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 ACCESS
Package A Package
B
Any Class
Sub Class B
A
Sub Class
A
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 ACCESS
Package A Package
B
Any Class A
Sub Class B
Sub Class A
Bahria University Karachi Campus
• For Example,
int val;
void display();
Bahria University Karachi Campus
DEFAULT ACCESS
Package A Package B
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 ACCESS
Package A Package B
Sub Class B
Any Class A
Sub Class A
Bahria University Karachi Campus
class Course {
// Data Member
private String studentName;
private String courseCode ;
}
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