Unit1 Principles of OOPS 2152022 By Kanika Sharma

Unit-1 Principle's of OOP’S 2/15/2022 By: Kanika Sharma

Contents • Introduction • Procedural Vs. OOP • Object Oriented Programming Paradigms: • • Classes Objects Data Abstraction Encapsulation Inheritance Polymorphism Dynamic Binding Message Passing • Object Oriented Languages • Object Based Languages • Benefits of OOP’s 2/15/2022 By: Kanika Sharma

Procedural Programming • In a procedural program data is typically stored in a collection of variables and there is a set of functions that perform operations on the data. • The data and the functions are separate entities. Usually the variables are passed to the functions that perform the desired operations. • As you might imagine, the focus of procedural programming is on creating the functions, or procedures, that operate on the program’s data. • Procedural programming works well. However, as programs become larger and more complex, the separation of a program’s data and the code that operates on the data can lead to problems. 2/15/2022 By: Kanika Sharma

Object Oriented Programming • The object oriented programming design models the real world well and overcomes the shortcomings of procedural paradigm. It views a problem in terms of objects and thus emphasizes on both procedures as well as data. • An object is an entity that combines both data and procedures in a single unit. An object’s data items, also referred to as its attributes, are stored in member variables. The procedures that an object performs are called its member functions. This wrapping of an object’s data and procedures together is called encapsulation. • Not only objects encapsulate associated data and procedures, they also permit data hiding. Data hiding refers to an object’s ability to hide its data from code outside the object. Only the object’s member functions can directly access and make changes to the object’s data. 2/15/2022 By: Kanika Sharma

Procedural vs. Object-Oriented Procedural application DATA Line of code Data is stored Line of code independent Line of code of application OO-application Each object is independent of the others 2/15/2022 By: Kanika Sharma

Procedural Vs. OOP’s 2/15/2022 By: Kanika Sharma

Object Oriented Paradigm (Paradigm: a way of seeing and doing things) • Object - Oriented (OO) Programming: • Organizing software as a collection of objects with a certain state and behavior. • Object Oriented Design: • Based on the identification & organization of objects. • OO Methodology • Construction of models • The development of SW is a modeling process • OO Modeling and Design • Modeling objects based on the real world • Using models to design independently of a programming 2/15/2022 By: Kanika Sharma language.

Objects • Object: Complex data type that has an identity, contains other data types called attributes and modules of code called operations or methods • Attributes and associated values are hidden inside the object. • Any object that wants to obtain or change a value associated with other object, must do so by sending a message to one of the objects (invoking a method) 2/15/2022 By: Kanika Sharma

Objects methods (methods) method: Set_salary Object: woman Age: 35 Salary: 10 Get_age Set_salary (20) employer 2/15/2022 Attributes (values) friend By: Kanika Sharma

Classes • Classes are templates that have methods and attribute names and type information, but no actual values! • Objects are generated by these classes and they actually contain values. • We design an application at the class level. • When the system is running objects are created by classes as they are needed to contain state information. • When objects are no longer needed by the application, they are eliminated. 2/15/2022 By: Kanika Sharma

Public, Private & Protected • Attributes can be public or private: • Private: it can only be accessed by its own methods • Public: it can be modified by methods associated with any class (violates encapsulation) • Methods can be public, private or protected: • Public: it’s name is exposed to other objects. • Private: it can’t be accessed by other objects, only internally • Protected: (special case) only subclasses that descend directly from a class that contains it, know and can use this method. 2/15/2022 By: Kanika Sharma

Class & Objects CLASS: Furniture Name Number methods: Example Change. Number Objects: Desk 123445 2/15/2022 Chair. A 32143 By: Kanika Sharma Chair. B 45687

Memory allocation to members of class 2/15/2022 By: Kanika Sharma

Data Abstraction • Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i. e. , to represent the needed information in program without presenting the details. • Let's take one real life example of a TV, which you can turn on and off, change the channel, adjust the volume, and add external components such as speakers, VCRs, and DVD players, BUT you do not know its internal details, that is, you do not know how it receives signals over the air or through a cable, how it translates them, and finally displays them on the screen. 2/15/2022 By: Kanika Sharma

Encapsulation • Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. • Data encapsulation led to the important OOP concept of data hiding. • Each objects methods manage it’s own attributes. • This is also known as hiding. • An object A can learn about the values of attributes of another object B, only by invoking the corresponding method (message) associated to the object B. • Example: • Class: Lady • Attributes: Age, salary • Methods: get_age, set_salary 2/15/2022 By: Kanika Sharma

Class Hierarchies & Inheritance • Classes can be arranged in hierarchies so that more classes inherit attributes and methods from more abstract classes • Class hierarchy diagrams Class: Chair subclasses Chair Type A 2/15/2022 Chair Type B By: Kanika Sharma

Class Inheritance & Specialization Class name Methods Is a specialization of Or Inherits from 2/15/2022 Class: Furniture Attribute A 1 Method A 1 Class: Chairs [Attribute A 1] Attribute B 1 [Method A 1] Method B 1 Class: Executive Chairs [Attribute A 1] [Attribute B 1] Attribute C 1 [Method A 1] Method B 1 (B 1 code modified) Method C 1 By: Kanika Sharma Attributes

Message Passing & Associations • Methods are associated with classes but classes don’t send messages to each other. • Objects send messages. • A static diagram (class diagram) shows classes and the logical associations between classes, it doesn´t show the movement of messages. • An association between two classes means that the objects of the two classes can send messages to each other. • Aggregation: when an object contains other objects ( a part-whole relationship) 2/15/2022 By: Kanika Sharma

Polymorphism • Means that the same method will behave differently when it is applied to the objects of different classes • It also means that different methods associated with different classes can interpret the same message in different ways. • Example: an object can send a message PRINT to several objects, and each one will use it’s own PRINT method to execute the message. 2/15/2022 By: Kanika Sharma

Dynamic Binding • Dynamic binding also called dynamic dispatch is the process of linking procedure call to a specific sequence of code (method) at run-time. It means that the code to be executed for a specific procedure call is not known until run-time. Dynamic binding is also known as late binding or run-time binding. 2/15/2022 By: Kanika Sharma

Object Oriented Languages • Object-oriented programming (OOP) is a programming language model organized around objects rather than "actions" and data rather than logic. • Object-oriented programming (OOP) refers to a type of computer programming (software design) in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure. • In this way, the data structure becomes an object that includes both data and functions. In addition, programmers can create relationships between one object and another. For example, objects can inherit characteristics from other objects. 2/15/2022 By: Kanika Sharma

Object Based Languages • The term "object-based language" may be used in a technical sense to describe any programming language that uses the idea of encapsulating state and operations inside "objects". • Object-based languages need not support inheritance or subtyping, but those that do are also said to be "object-oriented". • Object-based languages that do not support inheritance or subtyping are usually not considered to be true objectoriented languages. 2/15/2022 By: Kanika Sharma

Object Based Vs. Object Oriented 2/15/2022 By: Kanika Sharma

Benefits of OOPs • • Software complexity can be easily managed. Object-oriented systems can be easily upgraded. It is quite easy to partition the work in a project based on object. Objects created for Object Oriented Programs can easily be reused in other programs. • Large programs are very difficult to write. Object Oriented Programs force designers to go through an extensive planning phase, which makes for better designs with less flaws. • Programs are not disposable. Legacy code must be dealt with on a daily basis, either to be improved upon (for a new version of an exist piece of software) or made to work with newer computers and software. An Object Oriented Program is much easier to modify and maintain than a non-Object Oriented Program. So although a lot of work is spent before the program is written, less work is needed to maintain it over time. 2/15/2022 By: Kanika Sharma
- Slides: 24