Escolar Documentos
Profissional Documentos
Cultura Documentos
Encapsulation
( Information Hiding )
Prepared by:Darshana Mistry
eInfochips Training and Research
Academy (eiTRA)
Objectives
1. Explanation of Encapsulation and Information
Hiding.
2. What encapsulation or information hiding approach
provide in Object Oriented ?
3. General 3 different ways to Encapsulate data.
4. Advantage of Encapsulation ( Information hiding ).
5. Example by encapsulation or information hiding.
6. Constructor
1. Types of constructor
Abstraction
Don't need
to know
this
Can focus
on this!!
Encapsulation
encapsulation: Hiding implementation details of an
object from its clients.
Encapsulation provides abstraction.
separates external view (behavior) from internal
view (state)
Encapsulation protects the integrity of an object's
data.
Encapsulation
to enclose in or as in a capsule
1. The object-oriented meaning of encapsulation is to enclose
related data, routines and definitions in a class capsule. This
does not necessarily mean hiding. (Mish)
2. Encapsulation is the bundling together of data and
behavior so that they are inseparable.
(Mcgraw Hill)
Information Hiding
1. Information hiding: a module is characterized by the
information it hides from other modules, which are called
its clients. The hidden information remains a secret to the
client modules.
(Ghezzi et al)
2. The purpose of Information hiding is to make inaccessible
certain details that should not affect other parts of a system.
(Ross et
al)
10
2.
11
Syntax
private: <declarations>
Description:
1. A private member can be accessed only by member
functions and friends of the class in which it is declared.
2. Class members are private by default.
3. You can override the default struct access with private or
protected but you cannot override the default union access.
4. Friend declarations are not affected by these access
specifiers.
12
class MyClass
{
public:
//access from anywhere
int x;
private:
//only access from within a class
int y;
protected:
//access from within a class ,or derived class
int z;
};
void main()
{
MyClass CopyClass;
CopyClass.x = 1; //OK, Public Access.
CopyClass.y = 2; //Error! Y isn't a member of MyClass
CopyClass.z = 3; //Error! Z isn't a member of MyClass
}
13
14
public:
Box(int, int, int); // constructor function.
};
~Box();
// destructor function.
int volume();
15
16
{
height = ht;
width = wd;
depth = dp;
}
Box::~Box()
{
// does nothing
}
int Box::volume()
{
return height * width * depth;
}
(cont.)
17
// actual values
return 0;
}
this
this : A reference to the implicit parameter.
implicit parameter: object on which a method is called
Variable shadowing
An instance method parameter can have the same
name as one of the object's fields:
// this is legal
public void setLocation(int x, int y) {
...
}
Fields x and y are shadowed by parameters with
same names.
Any setLocation code that refers to x or y will
use the parameter, not the field.
20
Multiple constructors
It is legal to have more than one constructor in a class.
The constructors must accept different parameters.
public class Point {
private int x;
private int y;
public Point() {
x = 0;
y = 0;
}
public Point(int initialX, int initialY) {
x = initialX;
y = initialY;
}
}
...
22
...
23
Example of Constructors
class box {
public:
box ()
{ i = 1; }
box (int x)
{ i = x; }
// default constructor
// give data field some default value
// ordinary constructor
Example of Constructors
class box {
public:
box ()
{ i = 1; }
box (int x)
{ i = x; }
// default constructor
// give data field some default value
// ordinary constructor
Order of Initialization
In C++, the initialization of parent classes occurs before the
initialization of child class.
Methods that are invoked are matched only to functions in the
parent class, even if these methods have been declared as
virtual.
26
Initialization in Java
class A { // Java classes illustrating initialization
public A () {
System.out.println("in A constructor");
init();
}
public void init () {
System.out.println ("in A init");
}
}
class B extends A {
public B () {
System.out.println ("in B constructor");
}
public void init () {
super.init();
System.out.println ("in B init");
}
}
27
Output of Java
in A constructor
in A init
in B init
in B constructor
28
Initialization in C++
class A { // C++ classes illustrating initialization
public:
A () {
printf("in A constructor\n");
init();
}
virtual void init () {
printf("in A init\n");
}
};
class B : public A {
public:
B () {
printf("in B constructor\n"); }
virtual void init () {
A::init();
printf("in B init\n");
}
};
29
Output of C++
in A constructor
in A init
in B constructor
30
Combining Constructors
In C++, you cannot invoke one constructor form
within another.
class box {
// error -- does not work as expected
public:
box (int i) : x(i) { }
box (int i, int j) : y(j) { box::box(i); }
int x, y;
};
31
Example of Constructors
// C++ class with default arguments in constructor
class newClass {
public:
newclass (int i, int j = 7)
{
// do object initialization
...
}
};
32
Example of Constructors
// C++ class with factored constructors
class newClass {
public:
newClass (int i)
{
initialize(i); // do common initialization
}
newClass (int i, int j) {
initialize(i);
... // then do further initialization
}
private:
void initialize (int i) {
... // common initialization actions
}
};
33
34