Introduction to Object Oriented Programming

Computer Programming 2 Programming Paradigm Procedural Programming ? ? style of programming in which the programming task is broken down into a series of operations (called procedures) applied to data (or data structures) C and Pascal Object-Oriented Programming ? ? ? extension of procedural programming breaks down a programming task into a series of interactions among different entities or objects Java, C++, and Smalltalk Introduction to OOP * Property of STI Page 1 of 15 Computer Programming 2 Object-Oriented Programming type of programming in which programmers define not only the data structures, but also the ypes of operations (methods) that can be applied to the data structure enables programmers to create modules that do not need to be changed when a new type of object is added most widely used paradigm instead of focusing on what the system has to do, focus on: ? what objects the system contains ? how they interact towards solving the programming problem Introduction to OOP * Property of STI Page 2 of 15 1 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ Computer Programming 2 Object-Oriented Programming Illustration of OOP Introduction to OOP * Property of STI Page 3 of 15 Computer Programming 2 Object-Oriented Programming Advantages of OOP over conventional approaches: ? It provides a clear modular structure for programs which makes it good for defining abstract data types where implementation details are hidden and the unit has a clearly efined interface. ? It makes it easy to maintain and modify existing code as new objects can be created with small differences from existing ones. ? It provides a good framework for code libraries where supplied software components can be easily adapted and modified by the programmer. This is particularly useful for developing graphical user interfaces. Introduction to OOP * Property of STI Page 4 of 15 3 __________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ __________________ 4 __________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ Computer Programming 2 Object-Oriented Programming Key OOP concepts: ? Objects ? Classes ? Abstraction ? Inheritance ? Encapsulation ? Polymorphism Introduction to OOP * Property of STI Page 5 of 15 Computer Programming 2 Objects and Classes Objects ? ? represent “things” from the real world made up of • attributes – characteristics that define an object methods – self-contained block of program code similar to procedure ? example: • a car’s attributes are make, model, year, and purchase price • a car’s methods are forward and backward Classes ? ? ? ? term that describes a group or collection of objects with common properties define a type of object specifies methods and data that type of object has example: • Employee • Car Introduction to OOP * Property of STI Page 6 of 15 5 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ __________________ 6 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ Computer Programming 2 Abstraction allows a programmer to hide all but the relevant information (to the problem at hand) about an object in order to reduce complexity and increase efficiency closely related to encapsulation and information hiding Introduction to OOP * Property of STI Page 7 of 15 Computer Programming 2 Encapsulation refers to the hiding of data (attributes) and ethods within an object protects an object’s data from corruption protects the object’s data from arbitrary and unintended use hides the details of an object’s internal implementation from the users of an object separates how an object behaves from how it is implemented easier to modify programs since one object type is modified at a time Introduction to OOP * Property of STI Page 8 of 15 7 __________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ __________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ Computer Programming 2 Encapsulation Illustration of Encapsulation Change Address Change Name View Name Enroll in a Course Name Address Course GPA Drop a Course View GPA Introduction to OOP * Property of STI Page 9 of 15 Computer Programming 2 Inheritance the process by which objects can acquire (inherit) the properties of objects of other class provides reusability, like adding additional eatures to an existing class without modifying it Introduction to OOP * Property of STI Page 10 of 15 9 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ 10 ________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ Computer Programming 2 Inheritance getName() etID() Name Student ID setName() setID() Student Introduction to OOP * Property of STI Page 11 of 15 Computer Programming 2 Polymorphism refers to the ability to process objects differently depending on their data type or class the ability to redefine methods for derived classes request for an operation can be made without knowing which specific method should be invoked Introduction to OOP * Property of STI Page 12 of 15 11 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ __________________ ___________________ 12 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ Computer Programming 2 Polymorphism Illustration of Polymorphism Student Application Form s. isRegistered() Car c. isRegistered() Introduction to OOP * Property of STI Page 13 of 15 Computer Programming 2 Abstract Classes class that is not used to create (instantiate) objects designed to act as a base class (to be inherited by other classes) esign concept in program development and provides a base upon which other classes are built can only specify members that should be implemented by all inheriting classes Introduction to OOP * Property of STI Page 14 of 15 13 ________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ 14 ________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ __________________ ___________________ ___________________ ___________________ Computer Programming 2 Interfaces allow you to create definitions for component interaction provide another way of implementing polymorphism specify methods that a component must implement without actually specifying how the method is implemented Introduction to OOP * Property of STI Page 15 of 15 15 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________