Download presentation
Presentation is loading. Please wait.
1
CS-2135 Object Oriented Programming
the UNIVERSITY OF LAHORE CS-2135 Object Oriented Programming Lecture 9: Encapsulation, Information Hiding & Introduction to Inheritance Course Instructor Mr. Junaid Aziz
2
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)
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)
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)
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)
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
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
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
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
10
Inheritance; 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: 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
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. Vehicle Car Truck Bus CS-2135 Object Oriented Programming (2015) 11
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
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
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. Parent Child CS-2135 Object Oriented Programming (2015) 14
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. Parent 1 Parent 2 Child CS-2135 Object Oriented Programming (2015) 15
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. Access Specifier Accessible from own class Accessible from derived class Accessible from objects outside the class PUBLIC YES PRIVATE NO PROTECTED CS-2135 Object Oriented Programming (2015) 16
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.