Top Banner
Encapsulation ( Information Hiding ) eInfochips Institute of Training Research and Academi Prepared by:- Darshana Mistry eInfochips Training and Research Academy (eiTRA) 1
34
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 02_Encapsulation

Encapsulation

( Information Hiding )

eInfochips Institute of Training Research and Academics Limited

Prepared by:- Darshana Mistry

eInfochips Training and Research Academy (eiTRA)

1

Page 2: 02_Encapsulation

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

2

Page 3: 02_Encapsulation

Abstraction

Don't need to know this

Can focus on this!!

3

Page 4: 02_Encapsulation

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.

4

Page 5: 02_Encapsulation

( Explanation of Encapsulation and Information Hiding )

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)

5

Page 6: 02_Encapsulation

Cont. Explanation of Encapsulation and Information Hiding

Why Encapsulation is also called information hiding ?

Encapsulation (also called information hiding) consists of separating the external aspects of an object, from the internal implementation details of the object, which are hidden from other objects.

Encapsulation prevents a program from becoming to interdependent that a small change has massive ripple effects.

Encapsulation has ability to combine data structure and behavior in a single entity makes encapsulation cleaner and more powerful than in conventional languages that separate data structure and behavior.

6

Page 7: 02_Encapsulation

Cont. Explanation of Encapsulation and Information Hiding

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)

3. Information hiding is the principle that users of a software component (such as a class) need to know only the essential details of how to initialize and access the component, and do not need to know the details of the implementation. (Budd)

Note: A class can act like Server, and a program “main” can act like Client.

7

Page 8: 02_Encapsulation

What encapsulation or information hiding approach provides in Object Oriented?

1. The interface is the visible surface of the capsule.

The interface describes the essential characteristics of objects of the class which are visible to the exterior world.

Interface data – which should be visible from outside/other class or method.

2. The implementation is hidden in the capsule.

The implementation hiding means that data can only be manipulated, that is updated, within the class, but it does not mean hiding interface data.

Implementation data – which should be hidden from outside/other class or method.

Note: A function is an interface and has function definition, and A class is an implementation and has its declaration includes all implementation details of data and function members.

8

Page 9: 02_Encapsulation

General 3 ways to Encapsulate Data

1. Public member access specifier

2. Private member access specifier

3. Protected member access specifier

9

Page 10: 02_Encapsulation

Cont. General 3 ways to Encapsulate Data

1. Public member access specifier.

Syntax

public: <declarations>

Description:

1. A public member can be accessed by any function.

2. Members of a struct or union are public 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.

10

Page 11: 02_Encapsulation

Cont. General 3 ways to Encapsulate Data

2. Private member access specifier

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.

11

Page 12: 02_Encapsulation

Cont. General 3 ways to Encapsulate Data

3. Protect member access specifier

Syntax

protected: <declarations>

Description:

1. A protected member can be accessed by member functions and friends of the class in which it was declared, and by classes derived (derived classes) from the declared class.

2. You can override the default struct access with private or protected but you cannot override the default union access.

3. Friend declarations are not affected by these access specifiers.

12

Page 13: 02_Encapsulation

Cont. General 3 ways to Encapsulate Data

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

Page 14: 02_Encapsulation

Advantage of Encapsulation ( Information hiding )

1. It prevents others accessing the insides of an object.

The only thing that can manipulate the data in an object is that object’s method or member function.

2. It main aim is to prevent accident.

It builds a protective wall (encapsulation) around the member data and member function of the class, and hiding implementation details of object. So It keeps data safe from accident.

14

Page 15: 02_Encapsulation

Example by encapsulation or information hiding

#include <iostream>

// Declaration of the Box class.

class Box

{

private:

int height, width, depth; // private data members.

public:

Box(int, int, int); // constructor function.

~Box(); // destructor function.

int volume(); // member function (compute volume).

};

(cont.)

15

Page 16: 02_Encapsulation

// Definition of the Box class.

Box::Box( int ht, int wd, int dp ) // The constructor function.

{

height = ht;

width = wd;

depth = dp;

}

Box::~Box() // The destructor function. Use of scope resolution ‘::’

{

// does nothing

}

int Box::volume() // Member function to compute the Box's volume.

{

return height * width * depth;

}

(cont.)

16

Page 17: 02_Encapsulation

// The main() function.

int main()

{

// Construct a Box object.

Box thisbox(7, 8, 9); // actual values

// Compute and display the object's volume.

int volume = thisbox.volume();

std::cout << “output volume is : “ << volume;

return 0;

}

output volume is: 504

17

Page 18: 02_Encapsulation

this

• this : A reference to the implicit parameter.– implicit parameter: object on which a method is called

• Syntax for using this:

– To refer to a field:this.field

– To call a method:this.method(parameters);

– To call a constructor from another constructor:this(parameters);

18

Page 19: 02_Encapsulation

Variable names and scope

• Usually it is illegal to have two variables in the same scope with the same name.

public class Point { int x; int y; ...

public void setLocation(int newX, int newY) { x = newX; y = newY; }}

– The parameters to setLocation are named newX and newY to be distinct from the object's fields x and y.

19

Page 20: 02_Encapsulation

Variable shadowing

• An instance method parameter can have the same name as one of the object's fields:

// this is legalpublic 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

Page 21: 02_Encapsulation

Avoiding shadowing w/ this

public class Point { private int x; private int y;

...

public void setLocation(int x, int y) { this.x = x; this.y = y; }}

• Inside the setLocation method,– When this.x is seen, the field x is used.– When x is seen, the parameter x is used.

21

Page 22: 02_Encapsulation

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

Page 23: 02_Encapsulation

Constructors and this

• One constructor can call another using this:

public class Point { private int x; private int y;

public Point() { this(0, 0); // calls the (x, y) constructor }

public Point(int x, int y) { this.x = x; this.y = y; }

...}

23

Page 24: 02_Encapsulation

24

Example of Constructors

class box {public:

box () // default constructor{ i = 1; } // give data field some default value

box (int x) // ordinary constructor{ i = x; }

box (const box & a) // copy constructor{ i = a.i; } // clone argument value

private:int i;

};box one; // default constructorbox two (7); // ordinary constructorbox three (two); // copy constructor

Page 25: 02_Encapsulation

25

Example of Constructors

class box {public:

box () // default constructor{ i = 1; } // give data field some default value

box (int x) // ordinary constructor{ i = x; }

box (const box & a) // copy constructor{ i = a.i; } // clone argument value

private:int i;

};box one; // default constructorbox two (7); // ordinary constructorbox three (two); // copy constructor

Page 26: 02_Encapsulation

26

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.

Page 27: 02_Encapsulation

27

Initialization in Java

class A { // Java classes illustrating initializationpublic 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");

}}

Page 28: 02_Encapsulation

28

Output of Java

in A constructor

in A init

in B init

in B constructor

Page 29: 02_Encapsulation

29

Initialization in C++

class A { // C++ classes illustrating initializationpublic:

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

}};

Page 30: 02_Encapsulation

30

Output of C++

in A constructor

in A init

in B constructor

Page 31: 02_Encapsulation

31

Combining Constructors

• In C++, you cannot invoke one constructor form within another.

class box { // error -- does not work as expectedpublic:

box (int i) : x(i) { }box (int i, int j) : y(j) { box::box(i); }

int x, y;

};

Page 32: 02_Encapsulation

32

Example of Constructors

// C++ class with default arguments in constructor

class newClass {public:

newclass (int i, int j = 7) {

// do object initialization...

}

};

Page 33: 02_Encapsulation

33

Example of Constructors

// C++ class with factored constructorsclass 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}

};

Page 34: 02_Encapsulation

34

The Orthodox Canonical Class Form

• A default constructor: used internally to initialize objects and data members when no other value is available.

• A copy constructor: used in the implementation of call-by-value parameters.

• An assignment operator: used to assign one value to another.

• A destructor: Invoked when an object is deleted.