16
CS-2135 OBJECT ORIENTED PROGRAMMING Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz THE UNIVERSITY OF LAHORE

Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

Embed Size (px)

Citation preview

Page 1: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

CS-2135 OBJECT ORIENTED PROGRAMMING

Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance

Course Instructor

 

Mr. Junaid Aziz

THE UNIVERSITY OF LAHORE

Page 2: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

LECTURE OUTLINE:

Data Encapsulation & Information Hiding Access Specifiers & their Usage Introduction to Inheritance Purpose of Inheritance Advantages of Inheritance Categories of Inheritance

CS-2135 Object Oriented Programming (2015)

2

Page 3: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

3

DATA ENCAPSULATION:

As discussed earlier, all Object Oriented programs are composed of following two fundamental components:

Program statements (Functions): This is the part of a program that performs actions and they are called functions.

Program data: The data is the information of the program which is used by the program functions.

Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data,

Furthermore, its keeps both safe and hidden from illegal access and misuse.

This concept of data encapsulation lead us towards an important concept of data hiding.

This information hiding is considered as a good software engineering practice.

CS-2135 Object Oriented Programming (2015)

Page 4: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

4

DATA HIDING:

Process of hiding private or sensitive data from the client/user. Ability to protect the data from illegal access of unauthorized user.

In technical terms or Object Oriented methodology: Accessing the data members of a class is restricted to authorized functions. Client or user can’t directly interact with the data members. This is acquired by making use of Access Specifiers.

CS-2135 Object Oriented Programming (2015)

Page 5: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

5

DATA HIDING (CONTD):

Access Specifiers: Contain commands that are used to specify the access level for data

members of a class.

Types of Access Specifiers: ~

Private: Restrict the use of class member within the class. Member of the class declared with Private access specifier can only be

accessed by the functions declared within the class. Cannot be accessed outside the class. By default access specifier of a class member is Private. Data members of an object are mostly declared with Private access

specifier because they are quite sensitive. Used to protect the data from any direct access from outside the class.

CS-2135 Object Oriented Programming (2015)

Page 6: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

6

DATA HIDING (CONTD):

Public: Allow the user to access the class members within the class and outside the

class. Can be used anywhere in the program. All of the data and operations are accessible outside the scope of the class.

Protected: Allow the user to access the class members within the class and the class

that inherits it Can be accessed by the friends of this class and friends of its derived class. Same as private but with a little leniency towards the derived classes.

CS-2135 Object Oriented Programming (2015)

6

Page 7: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

7

DATA ENCAPSULATION & DATA HIDING (EXAMPLE) The concept of encapsulation and data hiding are practically implemented

in C++. This is acquired by the creation of user-defined types, called classes. A C++ program where you implement or define a class with public and

private members is an example of data encapsulation and data Hiding.

Consider the following example:

class Box { public: double getVolume(void) { return length * breadth * height; }

private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box

}; continued to next slide:

CS-2135 Object Oriented Programming (2015)

7

Page 8: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

8

DATA ENCAPSULATION & DATA HIDING (EXAMPLE) The access specifier of variables length, breadth, and height are private. This means that they can be accessed only by other members of the Box

class, and not by any other part of your program. This is one way through encapsulation is achieved.

CS-2135 Object Oriented Programming (2015)

8

Page 9: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

9

INTRODUCTION TO INHERITANCE: A technique in which we create a class that absorbs an existing class’s

capabilities, then further change and enhance them. A programming technique that is used to re use an existing class to build a

new class. The new class inherits all the behaviours or members functions of the

original class.

Base Class & Derived Class: An existing class that is reused to create a new class is known as base

class. Also called super class or parent class.

Sub Class: A new class that inherits or access the properties and functions of an

existing class. Also called derived class or child class. A derived class share the common properties and functions of its parent

class.CS-2135 Object Oriented Programming

(2015)9

Page 10: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

10

INHERITANCE; A REAL-WORLD EXAMPLE:

A Real-World Example Let's say that you are working for a vehicle parts manufacturer that needs

to update it's online inventory system. Your boss tells you to program two similar but separate forms for a website, one form that processes information about cars and one that does the same for trucks.

On evaluating the above mentioned scenario, we can define the classes mentioned below:

1) A parent class Vehicle having the following attributes:

2) The possible subclasses of this class Vehicle would be Cars, Truck & Bus.

These subclasses would share some similar properties such as: Color Engine Size Transmission Type Model

CS-2135 Object Oriented Programming (2015)

10

Page 11: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

11

INHERITANCE; A REAL-WORLD EXAMPLE (CONTD):Class Hierarchy: The relationship of inheritance between the classes of a program is called a

class hierarchy. In this relationship, a class can either become a base class or a derived

class. Consider the following diagram:

The above mentioned figure shows a simple inheritance hierarchy. A vehicle could be of various types such as: Car, Truck & Bus. It shows that Vehicle is a parent class. Bus, Truck and Car are subclass of Vehicle since they are derived from

the parent class Vehicle.CS-2135 Object Oriented Programming

(2015)11

Vehicle

TruckCar Bus

Page 12: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

12

INHERITANCE; A REAL-WORLD EXAMPLE (CONTD):

The upward arrow sign indicates that the subclasses are derived from the parent class Vehicle.

Moreover, an is-a relationship is used to represent inheritance. In this relationship, an object of a derived class are also an object its parent

class. For instance: a car is-a Vehicle, therefore all the attributes and behaviours

of a Vehicle are also attributes and behaviours of a Car/Bus/Truck.

CS-2135 Object Oriented Programming (2015)

12

Page 13: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

13

ADVANTAGES OF INHERITANCE:1) Reusability: Enables the programmer to reuse the existing code. Can reuse an existing class to create many sub classes.

2) Time Efficiency: Saves substantial time and effort involved in writing same kind of classes.

3) Increased Reliability & effective program structure: A parent class is already compiled and tested properly. It can used again in another application without testing and verifying again. This further increase the effectiveness of the program.

CS-2135 Object Oriented Programming (2015)

13

Page 14: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

14

CATEGORIES OF INHERITANCE:

1) Single Inheritance: A type of inheritance in which a new child class is derived from a single

parent class. The child class inherits or share all data members and member functions of

the parent class. The child class may contain its own members.

CS-2135 Object Oriented Programming (2015)

14

Parent

Child

Page 15: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

15

CATEGORIES OF INHERITANCE:

1) Multiple Inheritance: A type of inheritance in which a new child class is derived from multiple

parent classes. The child class inherits or share all data members and member functions of

the parent classes. The child class may contain its own members.

CS-2135 Object Oriented Programming (2015)

15

Parent 1

Child

Parent 2

Page 16: Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz

16

PROTECTED ACCESS SPECIFIER:

As discussed earlier, the private data members of a class can be accessed within the class in which they are declared.

Public data members can be accessed anywhere in the program. On the other hand, protected data members are specifically used for inheritance. These data members can be accessed from all the derived classes but not from

anywhere in the program. A base class’s protected members can be accessed within the body of that base

class by members and friends of that base class, and by members and friends of any classes derived from that base class. The following table shows the difference between all three access specifiers.

CS-2135 Object Oriented Programming (2015)

16

Access Specifier Accessible from own class

Accessible from derived class

Accessible from objects outside the class

PUBLIC YES YES YES

PRIVATE YES NO NO

PROTECTED YES YES NO