[oop - lec 08] encapsulation (information hiding)

14
Encapsulation (Information Hiding) Speaker: Muhammad Hammad Waseem [email protected]

Upload: muhammad-hammad-waseem

Post on 06-Apr-2017

149 views

Category:

Education


2 download

TRANSCRIPT

Page 1: [OOP - Lec 08] Encapsulation (Information Hiding)

Encapsulation (Information Hiding)

Speaker: Muhammad Hammad [email protected]

Page 2: [OOP - Lec 08] Encapsulation (Information Hiding)

Encapsulation• “ to enclose in or as in a capsule ”• 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)

• Encapsulation is the ‘bundling together’ of data and behavior so that they are inseparable. (Mcgraw Hill)

Page 3: [OOP - Lec 08] Encapsulation (Information Hiding)

Why Encapsulation 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.

Page 4: [OOP - Lec 08] Encapsulation (Information Hiding)

Information Hiding• 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)• 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)

Page 5: [OOP - Lec 08] Encapsulation (Information Hiding)

What Encapsulation Provides in OO? • 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.

• 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.

Page 6: [OOP - Lec 08] Encapsulation (Information Hiding)

General 3 Ways to Encapsulate DataPublic Member Access SpecifierPrivate Member Access SpecifierProtected Member Access Specifier

Page 7: [OOP - Lec 08] Encapsulation (Information Hiding)

Public Member Access Specifier• Syntax• public: <declarations>

• Description: • A public member can be accessed by any function.• Members of a struct or union are public by default.• You can override the default struct access with private or protected but you

cannot override the default union access.• Friend declarations are not affected by these access specifiers.

Page 8: [OOP - Lec 08] Encapsulation (Information Hiding)

Private Member Access Specifier• Syntax• private: <declarations>

• Description:• A private member can be accessed only by member functions and friends of

the class in which it is declared. • Class members are private by default.• You can override the default struct access with private or protected but you

cannot override the default union access.• Friend declarations are not affected by these access specifiers.

Page 9: [OOP - Lec 08] Encapsulation (Information Hiding)

Protect Member Access Specifier• Syntax• protected: <declarations>

• Description:• 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.• You can override the default struct access with private or protected but you

cannot override the default union access.• Friend declarations are not affected by these access specifiers.

Page 10: [OOP - Lec 08] Encapsulation (Information Hiding)

Example: Access Specifiersclass 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}

Page 11: [OOP - Lec 08] Encapsulation (Information Hiding)

Advantage of Encapsulation• 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.

• 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.

Page 12: [OOP - Lec 08] Encapsulation (Information Hiding)

Example: Encapsulation Or Information Hiding (1/3)#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).};

Page 13: [OOP - Lec 08] Encapsulation (Information Hiding)

Example: Encapsulation Or Information Hiding (2/3)// 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;}

Page 14: [OOP - Lec 08] Encapsulation (Information Hiding)

Example: Encapsulation Or Information Hiding (3/3)// 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(); cout << “output volume is : “ << volume;

return 0;}

RESULT:

output volume is: 504