0% found this document useful (0 votes)
10 views18 pages

Chapter5 1-ClassesAndObjectsInDepth

Chapter 5 covers the principles of classes and objects in object-oriented programming, focusing on information hiding and encapsulation. It explains how data can be protected through private and public modifiers, and how UML diagrams represent class visibility. The chapter also discusses the importance of abstraction, message passing, and the role of methods, constructors, and overloading in class design.

Uploaded by

salmaalsaeed3
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)
10 views18 pages

Chapter5 1-ClassesAndObjectsInDepth

Chapter 5 covers the principles of classes and objects in object-oriented programming, focusing on information hiding and encapsulation. It explains how data can be protected through private and public modifiers, and how UML diagrams represent class visibility. The chapter also discusses the importance of abstraction, message passing, and the role of methods, constructors, and overloading in class design.

Uploaded by

salmaalsaeed3
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/ 18

Chapter 5: Classes and Objects

in Depth

Information Hiding
Objectives
•Information hiding principle
•Modifiers and the visibility
•UML representation of a class
•Methods
•Message passing principle
•Passing parameters
•Getters and setters
•Constructors
•Overloading

Page 2 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Object Oriented Basic Principles

• Abstraction • Inheritance
• Encapsulation • Overriding
• Information Hiding • Polymorphism
• Message Passing • Dynamic Binding
• Overloading
• Information hiding and Message passing are
discussed in this chapter.
• Overloading is discussed in chapter 6.
• Inheritance, Polymorphism, Overriding and
Dynamic binding are discussed in CSC 113.
Page 3 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Abstraction Principle
• Data Abstraction • Functionality Abstraction
– In order to process – Modeling functionality
something from the real suffers from
world we have to extract • unnecessary functionality
the essential characteristics may be extracted,
of that object. • or alternatively, an
– Data abstraction is the important piece of
process of: functionality may be
omitted.
• Refining away the
unimportant details of an – Functionality abstraction is
object, the process of determining
• Keeping only the useful which functionality is
characteristics that define important. view
the object.
– For example, depending on
how a car is viewed (e.g. in
terms of something to be
registered, or alternatively
something to be repaired, view view
etc.) different sets of
characteristics will emerge
as being important.
Page 4 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Encapsulation Principle
• Abstraction involves reducing a real world
entity to its abstraction essential defining
characteristics.

• Encapsulation extends this idea by also


modeling and linking each data of an entity
to the appropriate functionality of that
entity.

Page 5 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Encapsulation Gives Classes
• OOP makes use of • Encapsulation is the OO
encapsulation to ensure that principle that allows objects
data is used in an appropriate containing the appropriate
manner. operations that could be applied
– by preventing from on the data they store.
accessing data in a non-
intended manner (e.g. asking – My Nokia-N71 cell-phone
if an Integer is true or false, stores:
etc.). • My contacts,
• Missed calls
• Through encapsulation, only a • … etc.
predetermined appropriate
group of operations may be – My Nokia-N71 may perform the
applied (have access) to the following operations on the data
data. it contains:
• Edit/Update/Delete an existing
contact
• Place data and the operations • Add a new contact
that act on that data in the • Display my missed calls.
same class. • …etc.

Page 6 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Information Hiding Principle

• Limit access to data only to internal


operations that need it.

• OO classes hide the data as private


data members and use public accessor
operations to get at it.
• The scope of the data is limited to the class.

Page 7 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Information Hiding Objectives

• Information hiding protects from


exposing:
• data items (attributes).
• the difference between stored data and
derived data.
• the internal structure of a class.
• implementation details of a class.

Page 8 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Encapsulation and
Information Hiding
• Encapsulation (is a language construct that )
facilitates the bundling of data with the
operations acting on that data.
• Place data and the operations that perform on
that data in the same class
• Information hiding is a design principle that
strives to shield client classes from the
internal workings of a class.
• Encapsulation facilitates, but does not
guarantee, information hiding.
• Smearing the two into one concept prevents
a clear understanding of either.

Page 9 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


public and private modifiers
• Let’s consider a class X.
• Let’s consider Y a client class of X.
• Y is a class that uses X.
• Attributes (and methods) of X declared with the
public modifier are accessible from instances of
Y.
• The public modifier does not guarantee the information
hiding.
• Attributes (and methods) of X declared with the
private modifier are not accessible from
instances of Y.
• The private modifier guarantee the information hiding.

Page 10 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Accessibility from Inside
(the Instance itself)
object:X

public
- Accessible
- Inaccessible
private

All members of an instance


are accessible from the
instance itself.

Page 11 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Accessibility from
an Instance of another Class
object:X
Accessibility from public
- Accessible
The Client class. - Inaccessible
private
:Y(client)

Only public members


Are visible from outside.
All else is hidden from
Outside.

Page 12 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Accessibility from
an Instance of the same Class
one:X
Accessibility from public
- Accessible
The Client class. - Inaccessible
private
two:X

If a member is accessible
from an instance, it is also
accessible from other
instances of the same
class.

Page 13 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


UML Representation of a Class
(UML Class Diagram)
• UML uses three symbols to represent the visibility
of the class’ members.
• + : mentions that the member is public.
• - : mentions that the member is private.
• # : introduced in the CSC 113.

ClassName
- att1: dataType1
-… Attributes
- atti: dataTypei

+ m1(…): dataType1 Methods


+ ... (Services)
+ mj(…): dataTypej

Page 14 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Declaring Private Attributes

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

Modifiers
Modifiers Data
DataType
Type Name
Name

private String studentName ;

Page 15 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Example of a Class with
Private attributes
ClassName
- studentName: String
- courseCode: String

public class Course {


// Attributes
private String studentName;
private String courseCode ;
// No method Members
}

Page 16 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


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= “CSC112“;
course1.studentName= “Majed AlKebir“;

//Create and assign values to course2


course2 = new Course( );
course2.courseCode= “CSC107“;
course2.studentName= “Fahd AlAmri“;

System.out.println(course1.studentName + " has the course “+


course1.courseCode);
System.out.println(course2.studentName + " has the course “+
course2.courseCode);

}
}

Page 17 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Accessibility Example

… class Service {
public int memberOne;
Service obj = new Service();
private int memberTwo;

obj.memberOne = 10; public void doOne() {



obj.memberTwo = 20; }
private void doTwo() {
obj.doOne(); …
}
obj.doTwo();
}

Client Service

Page 18 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

You might also like