Object Oriented Analysis and Design MCA 208 Prerequisite

Object Oriented Analysis and Design MCA 208 Pre-requisite based Study Material © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 1

Pre- Requisite Modules • Object Oriented Analysis and Design, NPTEL Instructor– Prof. Partha Pratim Das, Department of Computer Science and Engineering, IIT Kharagpur Duration – 08 Weeks Link – https: //nptel. ac. in/courses/106105153/ • Lecture Slides Pre-requisite based Study Material, Object Oriented Analysis and Design, by Tanya Pathak Garg © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 2

Evolution of Object Orientation • The idea of object-oriented programming gained momentum in the 1970 s and in the early 1980 s. • Bjorn Stroustrup integrated object-oriented programming into the C language. The resulting language was called C++ and it became the first object-oriented language to be widely used commercially. • In the early 1990 s a group at Sun led by James Gosling developed a simpler version of C++ called Java that was meant to be a programming language for video-on-demand applications. • This project was going nowhere until the group re-oriented its focus and marketed Java as a language for programming Internet applications. • The language has gained widespread popularity as the Internet has boomed, although its market penetration has been limited by its inefficiency. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 3

Evolution of Object Orientation 1. Monolithic Programming Approach: In this approach, the program consists of sequence of statements that modify data. • All the statements of the program are Global throughout the whole program. The program control is achieved through the use of jumps i. e. goto statements. • In this approach, code is duplicated each time because there is no support for the function. Data is not fully protected as it can be accessed from any portion of the program. • So this approach is useful for designing small and simple programs. The programming languages like ASSEMBLY and BASIC follow this approach. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 4

Evolution of Object Orientation © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 5

Evolution of Object Orientation 2. Procedural Programming Approach: This approach is top down approach. In this approach, a program is divided into functions that perform a specific task. • This approach avoids repetition of code which is the main drawback of Monolithic Approach. • The basic drawback of Procedural Programming Approach is that data is not secured because data is global and can be accessed by any function. • This approach is mainly used for medium sized applications. The programming languages: FORTRAN and COBOL follow this approach. • 3. Structured Programming Approach: The basic principal of structured programming approach is to divide a program in functions and modules. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 6

Evolution of Object Orientation © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 7

Evolution of Object Orientation • The use of modules and functions makes the program more comprehensible (understandable). It helps to write cleaner code and helps to maintain control over each function. This approach gives importance to functions rather than data. • It focuses on the development of large software applications. The programming languages: PASCAL and C follow this approach. 4. Object Oriented Programming Approach: The basic principal of the OOP approach is to combine both data and functions so that both can operate into a single unit. Such a unit is called an Object. • This approach secures data also. Now a days this approach is used mostly in applications. The programming languages: C++ and JAVA follow this approach. Using this approach we can write any lengthy code. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 8

Object Orientation Paradigm • An approach to the solution of problems in which all computations are performed in context of objects. • The objects are instances of programming constructs, normally called as classes which are data abstractions with procedural abstractions that operate on objects. • A software system is a set of mechanism for performing certain action on certain data Algorithm + Data structure = Program • Data Abstraction + Procedural Abstraction © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 9

Trade-offs of a Programming • Ease-of-use versus power • Safety versus efficiency • Rigidity versus extensibility © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 10

Object & Classes • Class is at core of Java • Any concept implemented in Java prg is encapsulated within class • Class define new data type which is used to create object of that type. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 11

Classes – The Blueprint !! • A class is a blueprint of an object. • A class is a group of objects that share common properties & behavior/ relationships. • In fact, objects are the variables of the type class. • Classes are user defined data types and behaves like the built-in types of a programming language. • Class are a concept, and the object is the embodiment of that concept. • Each class should be designed and programmed to accomplish one, and only one, thing, in accordance to single responsibility principle of SOLID design principles. • In the OOPs concept the variables declared inside a class are known as "Data Members" and the functions are known as "Member Functions" © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 12

Class Members • A class has different members, and developers in Microsoft suggest to program them in the following order: • Namespace: The namespace is a keyword that defines a distinctive name or last name for the class. A namespace categorizes and organizes the library (assembly) where the class belongs and avoids collisions with classes that share the same name. • Class declaration: Line of code where the class name and type are defined. • Fields: Set of variables declared in a class block. • Constants: Set of constants declared in a class block. • Constructors: A method or group of methods that contains code to initialize the class. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 13

Class Members • Properties: The set of descriptive data of an object. • Events: Program responses that get fired after a user or application action. • Methods: Set of functions of the class. • Destructor: A method that is called when the class is destroyed. In managed code, the Garbage Collector is in charge of destroying objects; however, in some cases developers need to take extra actions when objects are being released, such as freeing handles or deallocating unmanaged objects. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 14

Classes – A Classification © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 15

Defining Classes. . • Initializing data fields § By setting a value in a constructor § By assigning a value in the declaration § An initialization block • When constructor is called § All dat fields are initialized to their default values § All fields initializers and initialization blocks are executed, in the order in which they occur in the class declaration § If the first line of the constructor calls a second constructor, then the body of the second constructor is executed § The body of the constructor is executed © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 16

Object- The CRUX of the matter!! o “An object is an entity which has a state and a defined set of operations which operate on that state. ” o The state is represented as a set of object attributes. The operations associated with the object provide services to other objects (clients) which request these services when some computation is required o Objects are created according to some object class definition. An object class definition serves as a template for objects. It includes declarations of all the attributes and services which should be associated with an object of that class. o An Object is anything, real or abstract, about which we store data and those methods that manipulate the data. o An object is a component of a program that knows how to perform certain actions and how to interact with other elements of the program. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 18

Object- The CRUX of the matter!! • Each object is an instance of a particular class or subclass with the class's own methods or procedures and data variables. An object is what actually runs in the computer. • Objects are the basic run time entities in an object oriented system. • They match closely with real time objects. • Objects take up space in memory and have an associated address like a Record in Pascal and a Structure in C. • Objects interact by sending Message to one other. E. g. If “Customer” and “Account” are two objects in a program then the customer object may send a message to the account object requesting for bank balance without divulging the details of each other’s data or code. • Code in object-oriented programming is organized around objects. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 19

Object- A representation © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 20

Object- Attributes and Methods Object’s Attributes represented by data type. They describe objects states. In the Car example the car‘s attributes are: color, manufacturer, cost, owner, model, etc. Object’s Methods define objects behavior and specify the way in which an Object‘s data are manipulated. In the Car example the car‘s methods are: drive it, lock it, carry passenger in it. Objects- blueprints of classes The role of a class is to define the state and behavior of its instances. The class car, for example, defines the property color. Each individual car will have property such as "maroon, " "yellow" © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 21

Packages • Grouping of classes • Standard Java packages are inside java and javax • A class can use all classes from its own package and all public classes from other packages • Import a specific class or entire package using import statement • Locating classes in package is an activity of package © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 22

Packages. . • Static Imports § In Java SE 5. 0, import statement enhanced to import static methods & fields import static java. lang. System. *; out. println(“---”); § Two practical uses üMathematical functions: static import of Math class sqrt(pow(x, 2)+pow(y, 2)) Math. sqrt(Math. pow(x, 2)+Math. pow(y, 2)) üCumbersome constants if (d. get(DAY_OF_WEEK) == MONDAY) if (d. get(Calender. DAY_OF_WEEK) == Calender. MONDAY) © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 23

Packages. . • Import ONLY imports public classes from the specified package • Classes which are not public cannot be referenced from outside their package. • There is no way to "import all classes except one" § import either imports a single class or all classes within the package § Note: importing has no runtime or performance implications. § It is only importing a namespace so that the compiler can resolve class names. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 24

Packages. . • Addition of a class into a Package • Put the name of the package at the top of the calss • No package name, source file belong to default package © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 25

Object Orientated Features © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 26

Object Orientated Features Object orientation adapts to the following criteria's 1. Changing requirements 2. Easier to maintain 3. More robust 4. Promote greater design 5. Code reuse 6. Higher level of abstraction 7. Encouragement of good programming techniques 8. Promotion of reusability © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 27

Object Orientated Features 1. OBJECT - Object is a collection of number of entities. Objects take up space in the memory. Objects are instances of classes. When a program is executed , the objects interact by sending messages to one another. Each object contain data and code to manipulate the data. Objects can interact without having know details of each others data or code. Each instance of an object can hold its own relevant data. 2. CLASS - Class is a collection of objects of similar type. Objects are variables of the type class. Once a class has been defined, we can create any number of objects belonging to that class. Classes are user define data types. A class is a blueprint for any functional entity which defines its properties and its functions. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 28

Object Orientated Features 3. DATA ENCAPSULATION – Combining data and functions into a single unit called class and the process is known as Encapsulation. Class variables are used for storing data and functions to specify various operations that can be performed on data. This process of wrapping up of data and functions that operate on data as a single unit is called as data encapsulation. Data is not accessible from the outside world and only those function which are present in the class can access the data. 4. DATA ABSTRACTION- Abstraction (from the Latinn abs means away from and trahere means to draw) is the process of taking away or removing characteristics from something in order to reduce it to a set of essential characteristics. Advantage of data abstraction is security. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 29

Object Orientated Features 5. INHERITANCE- It is the process by which object of one class acquire the properties or features of objects of another class. The concept of inheritance provide the idea of reusability means we can additional features to an existing class without modifying it. This is possible by driving a new class from the existing one. Advantage of inheritance is reusability of the code. 6. MESSAGE PASSING - The process by which one object can interact with other object is called message passing. 7. POLYMORPHISM - A greek term means ability to take more than one form. An operation may exhibit different behaviours in different instances. The behaviour depends upon the types of data used in the operation. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 30

Object Orientated Features 8. PERSISTENCE - The process that allows the state of an object to be saved to non-volatile storage such as a file or a database and later restored even though the original creator of the object no longer exists. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 31

Inheritance • is-a relationship Class subclass-name extends superclass-name { // body of class } Subclass have more functionality then superclass • Each Java class has one (and only one) superclass • There is no limit to the number of subclasses a class can have • There is no limit to the depth of the class tree. © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 32

Inheritance. . • It is the responsibility of the subclass constructor to invoke the appropriate superclass constructors • Superclass constructors can be called using the "super" keyword in a manner similar to "this" • It must be the first line of code in the constructor • If a call to super is not made, the system will automatically attempt to invoke the no-argument constructor of the superclass. • Super has two general forms. § The first calls the superclass constructor § The second is used to access a member of the superclass that has been hidden by a member of a subclass • A superclass reference can refer to an instance of the superclass OR an instance of ANY class which inherits from the superclass. • Dynamic Method Dispatch will be applicable © Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Mrs. Tanya Pathak Garg, Asst. Prof. BVICAM PR. 33
- Slides: 32