390 likes | 680 Views
An Object-Oriented Approach to Programming Logic and Design. Chapter 2 Object-Oriented Programming Concepts. Objectives. Understand the basic principles of OOP Define classes and create class diagrams Understand public and private access Instantiate and use objects Understand inheritance.
E N D
An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts
Objectives • Understand the basic principles of OOP • Define classes and create class diagrams • Understand public and private access • Instantiate and use objects • Understand inheritance An Object-Oriented Approach to Programming Logic and Design
Objectives (continued) • Understand polymorphism • Understand protected access • Describe GUI classes as an example of built-in classes • Understand the advantages of OOP An Object-Oriented Approach to Programming Logic and Design
An Overview of Object-Oriented Programming • Object oriented programming: focuses on the data and the methods to manipulate it • Attributes: characteristics that define an object • Class: a group or collection of objects with common properties • Instance: an existing object created from a class An Object-Oriented Approach to Programming Logic and Design
An Overview of Object-Oriented Programming (continued) • Messages are passed to objects, requesting the objects to take actions • The same message works differently when applied to different objects • A module or procedure can process different types of data, without the need to write a separate version of the module or procedure An Object-Oriented Approach to Programming Logic and Design
An Overview of Object-Oriented Programming (continued) • Objects can share or inherit traits of other objects that have already been created • Encapsulation and information handling are important features of OOP • Methods: procedures that process data • Polymorphism: the ability to create multiple methods with the same name that act differently when used with different objects An Object-Oriented Approach to Programming Logic and Design
An Overview of Object-Oriented Programming (continued) • Inheritance: acquiring the traits of one’s predecessors • Encapsulation: combining an object’s attributes and methods into a single package • Information hiding: only an object’s own class should be able to alter that object’s attributes • Interface: the user interaction part of the object An Object-Oriented Approach to Programming Logic and Design
Defining Classes and Creating Class Diagrams • Class: a category of things • Object: a specific instance of a class • Class definition: set of program statements that detail the objects attributes and methods • A class may contain three parts: • Class name: required • Data: optional • Methods: optional An Object-Oriented Approach to Programming Logic and Design
Defining Classes and Creating Class Diagrams (continued) • Class diagram: rectangle with 3 sections • Class name • Attribute names and data types • Methods An Object-Oriented Approach to Programming Logic and Design
Defining Classes and Creating Class Diagrams (continued) An Object-Oriented Approach to Programming Logic and Design
Defining Classes and Creating Class Diagrams (continued) An Object-Oriented Approach to Programming Logic and Design
Defining Classes and Creating Class Diagrams (continued) An Object-Oriented Approach to Programming Logic and Design
Understanding Public and Private Access • Private access: data cannot be accessed by any method that is not part of the object’s class • Public access: other programs and methods may use the object’s methods • Access specifier: keyword that defines the access type • private • public An Object-Oriented Approach to Programming Logic and Design
Understanding Public and Private Access (continued) An Object-Oriented Approach to Programming Logic and Design
Understanding Public and Private Access (continued) • In class diagrams, access type is shown as a plus sign (+) for public, and a minus sign (–) for private An Object-Oriented Approach to Programming Logic and Design
Understanding Public and Private Access (continued) An Object-Oriented Approach to Programming Logic and Design
Instantiating and Using Objects • Instantiate – to create a class object, or instance of the class • Instantiation statement includes the class name and the object name Ex. Employee myAssistant • An instantiated object has its own copy of the attributes and methods defined for the class An Object-Oriented Approach to Programming Logic and Design
Instantiating and Using Objects (continued) An Object-Oriented Approach to Programming Logic and Design
Instantiating and Using Objects (continued) • Method call: statement that invokes a procedure (causes the procedure to execute) • Advantage of OOP is that the programmer does not need all of the details in the method, just needs to use it (feature of encapsulation) • A program that uses a class object is a client of the class An Object-Oriented Approach to Programming Logic and Design
Understanding Inheritance • Descendent (child) class: a new class that can inherit all of the attributes and methods of the original (parent) class, or can override the attributes and methods • Child class is more specific than the parent class An Object-Oriented Approach to Programming Logic and Design
Understanding Inheritance (continued) An Object-Oriented Approach to Programming Logic and Design
Understanding Inheritance (continued) An Object-Oriented Approach to Programming Logic and Design
Understanding Inheritance (continued) • Inherited methods that are modified in the child class but have the same name are said to overload or override the parent class methods • Inheritance allows re-use of code An Object-Oriented Approach to Programming Logic and Design
Understanding Inheritance (continued) An Object-Oriented Approach to Programming Logic and Design
Understanding Inheritance (continued) • Child class will use its parent class methods unless the child class overrides or overloads the methods • Abstract class: a class intended to be a parent only and not to have objects instantiated from it An Object-Oriented Approach to Programming Logic and Design
Using Polymorphism • Polymorphism: “many forms” of the same method • Same method call is carried out differently, depending on the context (usually, the type of data being used) • Each version of the method is written separately, but uses the same method name An Object-Oriented Approach to Programming Logic and Design
Understanding Protected Access • Protected Access: only child classes can use a data field marked as protected • Protected access is denoted in class diagrams with the octothorpe (#) sign An Object-Oriented Approach to Programming Logic and Design
Understanding Protected Access (continued) An Object-Oriented Approach to Programming Logic and Design
Understanding Protected Access (continued) An Object-Oriented Approach to Programming Logic and Design
Using a Predefined Class • Class Library: a collection of classes with related purposes • Example: classes for GUI components such as frames, buttons, labels, text boxes, etc. • Visual development environment used to create programs with a GUI user interface An Object-Oriented Approach to Programming Logic and Design
Understanding the Advantages of OOP • Saves programming time: • Objects instantiated from previously created classes include appropriate, reliable methods and attributes • Inheritance allows the extension of existing classes to serve related, more specific purposes • Pre-existing objects can be used as ‘black-box” components without needing to know the full details An Object-Oriented Approach to Programming Logic and Design
Summary • OOP focuses on the application’s data and the methods to manipulate the data • Object: consists of attributes and methods • Class: a collection of objects with common properties • Class definition and diagram has 3 parts: name, attributes, methods • Data hiding specifies that data can only be manipulated by the class that owns it An Object-Oriented Approach to Programming Logic and Design
Summary (continued) • Types of data access: public, private, protected • Instantiation: creation of an object from a class • Parent class properties can be inherited or overwritten/overloaded by child classes • Class library: collection of related classes • OOP can create reusable components An Object-Oriented Approach to Programming Logic and Design