THEORY OF PROGRAMMING LANGUAGES
Encapsulation & Inheritance
content
■ Explanation of Encapsulation and information hiding
■ General 3 different ways to Encapsulate data
■ Advantage of 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)
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.
Information Hiding
1. The purpose of Information hiding is to make inaccessible certain details that should not
affect other parts of a system.
(Ross et al)
2. 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)
General 3 ways to Encapsulate Data
■ Public member access specifier
■ Private member access specifier
■ Protected member access specifier
Public member access specifier
■ A public member can be accessed by any function.
SYNTAX:
public: <declarations>
Private member access specifier
■ 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.
SYNTAX:
private: <declarations>
Protected member access specifier
■ A protected member can be accessed by member functions and friends of the class in
which it was declared.
SYNTAX:
protected: <declarations>
For Example:
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;
};
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.
INHERITANCE
“the mechanism by which one class acquires
the properties of another class”
C++ and inheritance
■ The language mechanism by which one class acquires the properties (data and
operations) of another class.
■ Base Class (or superclass): the class being inherited from.
■ Derived Class (or subclass): the class that inherits
Advantages of
inheritance
When a class inherits from another class, there are three benefits:
■ You can reuse the methods and data of the existing class
■ You can extend the existing class by adding new data and new methods.
■ You can modify the existing class by overloading its methods with your own
implementations
Inheritance and
accessibility
■ A class inherits the behaviorof another class and enhances it in some way
■ Inheritance does not mean inheriting access to another class’ private members
■ Derived classes are special cases of base classes.
■ A derived class can also serve as a base class for new classes.
■ It is possible for a class to be a base class for more than one derived class .
Static vs. dynamic
binding
■ Static Binding:
Static binding occurs during the compile time.
■ Dynamic Binding:
Dynamic binding occurs during the run time.
Protected class
members
■ Derived classes cannot access the private data of the base class
■ Declaring methods and data of the base class as protected (instead of private)
allows derived classes to access them
■ Objects outside the class, however, cannot access them (same as private)
THANKYOU

Encapsulation and inheritance

  • 1.
  • 2.
  • 3.
    content ■ Explanation ofEncapsulation and information hiding ■ General 3 different ways to Encapsulate data ■ Advantage of Encapsulation ( Information hiding )
  • 4.
    ENCAPSULATION “ to enclosein 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)
  • 5.
    Why Encapsulation isalso 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.
  • 6.
    Information Hiding 1. Thepurpose of Information hiding is to make inaccessible certain details that should not affect other parts of a system. (Ross et al) 2. 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)
  • 7.
    General 3 waysto Encapsulate Data ■ Public member access specifier ■ Private member access specifier ■ Protected member access specifier
  • 8.
    Public member accessspecifier ■ A public member can be accessed by any function. SYNTAX: public: <declarations>
  • 9.
    Private member accessspecifier ■ 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. SYNTAX: private: <declarations>
  • 10.
    Protected member accessspecifier ■ A protected member can be accessed by member functions and friends of the class in which it was declared. SYNTAX: protected: <declarations>
  • 11.
    For Example: 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; };
  • 12.
    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.
  • 13.
    INHERITANCE “the mechanism bywhich one class acquires the properties of another class”
  • 14.
    C++ and inheritance ■The language mechanism by which one class acquires the properties (data and operations) of another class. ■ Base Class (or superclass): the class being inherited from. ■ Derived Class (or subclass): the class that inherits
  • 15.
    Advantages of inheritance When aclass inherits from another class, there are three benefits: ■ You can reuse the methods and data of the existing class ■ You can extend the existing class by adding new data and new methods. ■ You can modify the existing class by overloading its methods with your own implementations
  • 16.
    Inheritance and accessibility ■ Aclass inherits the behaviorof another class and enhances it in some way ■ Inheritance does not mean inheriting access to another class’ private members
  • 17.
    ■ Derived classesare special cases of base classes. ■ A derived class can also serve as a base class for new classes. ■ It is possible for a class to be a base class for more than one derived class .
  • 18.
    Static vs. dynamic binding ■Static Binding: Static binding occurs during the compile time. ■ Dynamic Binding: Dynamic binding occurs during the run time.
  • 19.
    Protected class members ■ Derivedclasses cannot access the private data of the base class ■ Declaring methods and data of the base class as protected (instead of private) allows derived classes to access them ■ Objects outside the class, however, cannot access them (same as private)
  • 20.