Sie sind auf Seite 1von 37

Bahria University Karachi Campus

CSC- 210

Object Oriented Programming

Lecturer

Sameena Javaid

https://sites.google.com/site/sameenajavaidcs

CSC- 210 Object Oriented Programming Lecturer Sameena Javaid https://sites.google.com/site/sameenajavaidcs
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
Bahria University Karachi Campus
LECTURE 4
ENCAPSULATION
(CLASSES AND OBJECTS)
OUTLINE
Class and Object (Encapsulation)
UML
Access modifiers

CLASS VS OBJECT

Bahria University Karachi Campus

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

• • • • • • • •
allocated Object: allocated Declaration Class: once • Object: many times • • • • • •
Bahria University Karachi Campus Declaring a Class with Java public class ClassName { // Attributes

Bahria University Karachi Campus

Declaring a Class with Java

public class ClassName {

// Attributes

// Constructor(s) // Methods (services)

{ // Attributes // Constructor(s) // Methods (services) Declaring Attributes With Java <modifiers> <data

Declaring Attributes With Java

<modifiers> <data type> <attribute name> ;

With Java <modifiers> <data type> <attribute name> ; Data Type Name public String studentName ;
With Java <modifiers> <data type> <attribute name> ; Data Type Name public String studentName ;
Data Type
Data Type
With Java <modifiers> <data type> <attribute name> ; Data Type Name public String studentName ;

Name

With Java <modifiers> <data type> <attribute name> ; Data Type Name public String studentName ;

public

String

studentName ;

} Modifiers
}
Modifiers
<data type> <attribute name> ; Data Type Name public String studentName ; } Modifiers

Bahria University Karachi Campus

OBJECT CREATION

Bahria University Karachi Campus OBJECT CREATION C o d e crs studentName courseCode Object: Course crscrs

Code

crs

crs studentName courseCode Object: Course crscrs Object: Course studentName courseCode

studentName

courseCode

Object: Course

crscrs

Object: Course

studentName

courseCode

d e crs studentName courseCode Object: Course crscrs Object: Course studentName courseCode State of Memory

State of Memory

A. The instance variable is allocated in memory. A B. The object is created B
A. The instance
variable is allocated
in memory.
A
B. The object is
created
B
Course crs;
new Course ( );
C. The reference of the
object created in B is
crs =
assigned to the variable.
C
B Course crs; new Course ( ); C. The reference of the object created in B

Bahria University Karachi Campus

Assigning Objects’ References to the same

Karachi Campus Assigning Objects’ References to the same A. The variable is allocated in memory. C
Karachi Campus Assigning Objects’ References to the same A. The variable is allocated in memory. C

A. The variable is allocated in memory.

Campus Assigning Objects’ References to the same A. The variable is allocated in memory. C o
Campus Assigning Objects’ References to the same A. The variable is allocated in memory. C o

Code

State of Memory

Instance Variable A Course crs; crs = new Course ( ); crs = new Course
Instance Variable
A
Course crs;
crs = new Course ( );
crs = new Course ( );
B
crs Course Course B. The reference to the new object is assigned to crs. C.
crs
Course
Course
B. The reference to the
new object is assigned
to crs.
C. The reference to
another object overwrites
the reference in crs.
Variable to Another crs1 crs2 A B Course crs1, crs2, crs1 = new Course( );
Variable to Another
crs1
crs2
A
B
Course crs1, crs2,
crs1 = new Course( );
crs2
= crs1;
C

Bahria University Karachi Campus

Assigning an Object Reference From One

Karachi Campus Assigning an Object Reference From One C o d e A. Variables are allocated
Karachi Campus Assigning an Object Reference From One C o d e A. Variables are allocated

Code

A. Variables are allocated in memory.

Course

B. The reference to the new

object is assigned to crs1 .

C. The reference in crs1 is assigned to crs2.

reference to the new object is assigned to crs1 . C. The reference in crs1 is

State of Memory

to the new object is assigned to crs1 . C. The reference in crs1 is assigned

Bahria University Karachi Campus

Assigning an Object Reference From One Variable to Another

Campus Assigning an Object Reference From One Variable to Another crs1 crs2 crs1 Course Course crs1
crs1 crs2 crs1 Course Course crs1 Course Course
crs1
crs2
crs1
Course
Course
crs1
Course
Course
A. Variables are allocated in memory. A A. Variables are assigned references of objects. Course
A. Variables are
allocated in memory.
A
A. Variables are
assigned references
of objects.
Course crs1, crs2,
B
crs2
crs1 = new Course( );
crs2 = new Course( );
C. The reference in crs2 is
assigned to crs1.
crs1
= crs2;
crs2
C
crs2 crs1 = new Course( ); crs2 = new Course( ); C. The reference in crs2

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 Object: Course studentName Maha AlSaad courseCode
course1
Object: Course
studentName
Maha AlSaad
courseCode

course1.studentName= “Sara Kabir; course2.studentName= Maha AlSaad;

course2

“ ; course2.studentName= “ Maha AlSaad “ ; course2 Object: Course studentName Sara Kabir courseCode

Object: Course

studentName

Sara Kabir

courseCode

 
“ Maha AlSaad “ ; course2 Object: Course studentName Sara Kabir courseCode  
“ Maha AlSaad “ ; course2 Object: Course studentName Sara Kabir courseCode  
Bahria University Karachi Campus class Course { // Instance attributes public String studentName; public String

Bahria University Karachi Campus

class Course {

// Instance attributes

public String studentName; public String courseCode ;

}

public String studentName; public String courseCode ; } public class CourseRegistration { public static void

public class CourseRegistration { public static void main(String[] args) { Course course1, course2; //Create and assign values to course1 course1 = new Course( ); course1.courseCode= new String(“CSC112“);

CourseRegistration

+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);

) ; System.out.println(course2.studentName + " has the course “+ } } course2.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.

with a 0x (for example, 0x334009). These addresses are unique for each object and are assigned
with a 0x (for example, 0x334009). These addresses are unique for each object and are assigned

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.

in the field of software engineering, that is intended to provide a standard way to visualize
in the field of software engineering, that is intended to provide a standard way to visualize

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.

coding of the system. UML was set up as a standardized model to describe an object-oriented
coding of the system. UML was set up as a standardized model to describe an object-oriented

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.

in list format, each operation takes up its own line. The operations describe how a class
in list format, each operation takes up its own line. The operations describe how a class

Bahria University Karachi Campus

CLASS DIAGRAM ANATOMY

Bahria University Karachi Campus CLASS DIAGRAM ANATOMY
Bahria University Karachi Campus CLASS DIAGRAM ANATOMY
Bahria University Karachi Campus CLASS DIAGRAM ANATOMY
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)

symbols: • Public (+) • Private (-) • Protected (#) • Package (~) • Derived (/)
symbols: • Public (+) • Private (-) • Protected (#) • Package (~) • Derived (/)
Examples Bahria University Karachi Campus

Examples

Bahria University Karachi Campus

Examples Bahria University Karachi Campus
Examples Bahria University Karachi Campus
Examples Bahria University Karachi Campus

Bahria University Karachi Campus

MODIFIERS

Bahria University Karachi Campus MODIFIERS
Bahria University Karachi Campus MODIFIERS

Bahria University Karachi Campus

ACCESS SPECIFIER (MODIFIERS)

A

classes, attributes, variables, methods and

mechanism

to

set

levels

for

access

constructors. The following are access levels:

static

public

private

protected

default

+

-

#

following are access levels: • • • • • static public private protected default + -
following are access levels: • • • • • static public private protected default + -

STATIC

Bahria University Karachi Campus

It used to define class attributes and methods. Class attributes (and methods):

live in the class

• Class attributes (and methods): live in the class are shared by all objects of the

are shared by all objects of the class.

do not belong to objects’ states.

<modifiers> <data type> <attribute name> ;

<modifiers> <data type> <attribute name> ; Data Type Name public static int studentNumber ;
Data Type
Data Type

Name

<modifiers> <data type> <attribute name> ; Data Type Name public static int studentNumber ;

public static

int

studentNumber ;

• • • Modifiers
Modifiers
<attribute name> ; Data Type Name public static int studentNumber ; • • • Modifiers

Bahria University Karachi Campus

CLASS ATTRIBUTES ACCESS

Class

be

manipulated without creating an instance of

the class.

attributes

(and

methods)

can

also

an instance of the class. attributes (and methods) can also <class name> . <attribute name> Class
an instance of the class. attributes (and methods) can also <class name> . <attribute name> Class

<class name>.<attribute name>

methods) can also <class name> . <attribute name> Class Name Attribute Name Course . studentNumber =
methods) can also <class name> . <attribute name> Class Name Attribute Name Course . studentNumber =
methods) can also <class name> . <attribute name> Class Name Attribute Name Course . studentNumber =
Class Name Attribute Name
Class Name
Attribute Name

Course.studentNumber = 0 ;

also <class name> . <attribute name> Class Name Attribute Name Course . studentNumber = 0 ;
also <class name> . <attribute name> Class Name Attribute Name Course . studentNumber = 0 ;

Bahria University Karachi Campus

CLASS ATTRIBUTES (SHARED MEMORY)

public class Student { static int count =0;

String id; String firstName; String lastName; String dateOfBirth;

}

Both student1 and student2 have a copy of these variables

All objects share variable count

count = 0

these variables All objects share variable count count = 0 student1 id firstName lastName dateOfBirth student2

student1

id

firstName

lastName

dateOfBirth

student2

id

firstName

lastName

dateOfBirth

Bahria University Karachi Campus public class Course { // attributes public String studentName; public String

Bahria University Karachi Campus

Bahria University Karachi Campus public class Course { // attributes public String studentName; public String
public class Course { // attributes public String studentName; public String courseCode ; public static
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;
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);
}
+ " has the course “+ course2.courseCode + “ ” + Course.studentNumber); }
+ " has the course “+ course2.courseCode + “ ” + Course.studentNumber); } }
+ " has the course “+ course2.courseCode + “ ” + Course.studentNumber); } }

}

PUBLIC ACCESS

Bahria University Karachi Campus

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();

outside the class. It can be accessed from outside package also. • For example, public int
the class. It can be accessed from outside package also. • For example, public int val;
the class. It can be accessed from outside package also. • For example, public int val;
B
B

PUBLIC ACCESS

Bahria University Karachi Campus

PUBLIC ACCESS Bahria University Karachi Campus Super Class A public members Any Class Sub Class A

Super Class A public members

Any Class

Sub Class

A

Package

Any Class B

Sub Class B

Package A A
Package A
A
Super Class A public members Any Class Sub Class A Package Any Class B Sub Class
Bahria University Karachi Campus public class Course { // attributes public String studentName; public String

Bahria University Karachi Campus

public class Course {

// attributes public String studentName; public String courseCode ;

}

public String studentName; public String courseCode ; } CourseRegistration public class CourseRegistration {
public String studentName; public String courseCode ; } CourseRegistration public class CourseRegistration {
CourseRegistration public class CourseRegistration { public static void main(String[] args) { Course course1, course2;
CourseRegistration
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“;
+main()
System.out.println(course1.studentName + " has the course “+
course1.courseCode );
System.out.println(course2.studentName + " has the course “+
course2.courseCode );
}
}
course1.courseCode ); System.out.println(course2.studentName + " has the course “+ course2.courseCode ); } }
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.

words, non-subclasses in other

packages cannot access protected members

from other packages. Means, this visibility control is strongly related to inheritance.

In

other

For example,

protected int val; protected void display();

control is strongly related to inheritance. In other For example, protected int val ; protected void
is strongly related to inheritance. In other For example, protected int val ; protected void display();
is strongly related to inheritance. In other For example, protected int val ; protected void display();
Bahria University Karachi Campus PROTECTED ACCESS Package A Super Class A protected members Any Class

Bahria University Karachi Campus

PROTECTED ACCESS

Bahria University Karachi Campus PROTECTED ACCESS Package A Super Class A protected members Any Class A

Package A

Super Class A protected members

Any Class A

Sub Class A

Package

B

Any Class B

Sub Class B

A Super Class A protected members Any Class A Sub Class A Package B Any Class
A Super Class A protected members Any Class A Sub Class A Package B Any Class

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();

is more restrictive than the protected member accessibility. • • • For Example, int val; void
is more restrictive than the protected member accessibility. • • • For Example, int val; void
is more restrictive than the protected member accessibility. • • • For Example, int val; void
DEFAULT ACCESS Bahria University Karachi Campus Package A Super Class A Members with no access

DEFAULT ACCESS

Bahria University Karachi Campus

DEFAULT ACCESS Bahria University Karachi Campus Package A Super Class A Members with no access modifier

Package A

Super Class A Members with no access modifier given

Any Class A

Sub Class A

Package B

Any Class B

Sub Class B

A Members with no access modifier given Any Class A Sub Class A Package B Any
A Members with no access modifier given Any Class A Sub Class A Package B Any

PRIVATE ACCESS

Bahria University Karachi Campus

This

visibility

controls.

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

is

most

restrictive

than

all

other

Private

members

are not accessible from

need to write ‘private’ in front of the member as,

private int val;

private void display();

accessible from • need to write ‘private’ in front of the member as, private int val;
from • need to write ‘private’ in front of the member as, private int val; private
from • need to write ‘private’ in front of the member as, private int val; private
PRIVATE ACCESS Bahria University Karachi Campus Package A Super Class A private members Any Class

PRIVATE ACCESS

Bahria University Karachi Campus

PRIVATE ACCESS Bahria University Karachi Campus Package A Super Class A private members Any Class A

Package A

Super Class A private members

Any Class A

Sub Class A

Package B

Any Class B

Sub Class B

Package A Super Class A private members Any Class A Sub Class A Package B Any
Package A Super Class A private members Any Class A Sub Class A Package B Any

Bahria University Karachi Campus

DECLARING PRIVATE ATTRIBUTES

String
String

<modifiers> <data type> <attribute name> ;

String <modifiers> <data type> <attribute name> ; Name Modifiers Data Type private studentName ;
Name
Name
Modifiers Data Type
Modifiers
Data Type

private

studentName ;

<data type> <attribute name> ; Name Modifiers Data Type private studentName ;
<data type> <attribute name> ; Name Modifiers Data Type private studentName ;

Bahria University Karachi Campus

EXAMPLE OF A CLASS WITH

Bahria University Karachi Campus EXAMPLE OF A CLASS WITH PRIVATE ATTRIBUTES Course - studentName: String -

PRIVATE ATTRIBUTES

Course - studentName: String - courseCode: String
Course
-
studentName: String
-
courseCode: String

public class Course {

// Attributes

private String studentName;

private String courseCode ; // No method Members

}

public class Course { // Attributes private String studentName; private String courseCode ; // No method
public class Course { // Attributes private String studentName; private String courseCode ; // No method
public class Course { // Attributes private String studentName; private String courseCode ; // No method
Bahria University Karachi Campus class Course { // Data Member private String studentName; private String

Bahria University Karachi Campus

Bahria University Karachi Campus class Course { // Data Member private String studentName; private String

class Course {

// Data Member

private String studentName; private String courseCode ;

}

String studentName; private String courseCode ; } public class CourseRegistration { public static void
public class CourseRegistration { public static void main(String[] args) { Course course1, course2; //Create and
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); }}
“+ course1.courseCode); System.out.println(course2.studentName + " has the course “+ course2.courseCode); }}
“+ course1.courseCode); System.out.println(course2.studentName + " has the course “+ course2.courseCode); }}

Bahria University Karachi Campus

ACCESSIBILITY EXAMPLE

Bahria University Karachi Campus ACCESSIBILITY EXAMPLE … Service obj = new Service(); obj.memberOne = 10;
Bahria University Karachi Campus ACCESSIBILITY EXAMPLE … Service obj = new Service(); obj.memberOne = 10;
… Service obj = new Service(); obj.memberOne = 10; obj.memberTwo = 20; obj.doOne(); obj.doTwo(); …
Service obj = new Service();
obj.memberOne = 10;
obj.memberTwo = 20;
obj.doOne();
obj.doTwo();
= 10; obj.memberTwo = 20; obj.doOne(); obj.doTwo(); … class Service { public int memberOne; private int

class Service {

public int

memberOne; private int memberTwo;

public void

doOne() {

}

private void

doTwo() {

}

}

Service

Client

private int memberTwo; public void doOne() { … } private void doTwo() { } … }
private int memberTwo; public void doOne() { … } private void doTwo() { } … }

Bahria University Karachi Campus

Bahria University Karachi Campus
Bahria University Karachi Campus
Bahria University Karachi Campus