CS-2135 Object Oriented Programming

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Final and Abstract Classes
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
Classes & Objects Computer Science I Last updated 9/30/10.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
2-1 © Prentice Hall, 2004 Chapter 2: Introduction to Object Orientation (Adapted) Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra,
INTRODUCTION TO JAVA PROGRAMMING Chapter 1. What is Computer Programming?
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Chapter 12: Adding Functionality to Your Classes.
11 1 Object oriented DB (not in book) Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Learning objectives: What.
1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 8 More Object Concepts
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Topics Inheritance introduction
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
Chapter 2: Introduction to Object Orientation Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich, Jeffrey A.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Object-Oriented Programming: Inheritance and Polymorphism.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
2-1 © Prentice Hall, 2004 Chapter 2: Introduction to Object Orientation Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object Oriented Programming
About the Presentations
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Week 4 Object-Oriented Programming (1): Inheritance
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
C++.
Object-Oriented Programming: Inheritance
Object Oriented Analysis and Design
Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Fundaments of Game Design
Inheritance and Polymorphism
Object Oriented Analysis and Design
Final and Abstract Classes
Object-Oriented Programming: Inheritance
Presentation transcript:

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

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)

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)

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)

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)

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

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

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

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

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

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

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

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

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

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

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