an object-oriented approach to programming logic and design chapter 2 object-oriented programming...

33
An Object-Oriented An Object-Oriented Approach to Approach to Programming Logic and Programming Logic and Design Design Chapter 2 Object-Oriented Programming Concepts

Upload: gwendoline-hines

Post on 28-Dec-2015

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to An Object-Oriented Approach to

Programming Logic and DesignProgramming Logic and Design

Chapter 2Object-Oriented Programming Concepts

Page 2: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 2

ObjectivesObjectives

• Understand the basic principles of OOP• Define classes and create class diagrams• Understand public and private access• Instantiate and use objects• Understand inheritance

Page 3: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 3

Objectives (continued)Objectives (continued)

• Understand polymorphism• Understand protected access• Describe GUI classes as an example of built-in

classes• Understand the advantages of OOP

Page 4: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 4

An Overview of Object-Oriented An Overview of Object-Oriented ProgrammingProgramming

• 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

Page 5: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 5

An Overview of Object-Oriented An Overview of Object-Oriented Programming (continued)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

Page 6: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 6

An Overview of Object-Oriented An Overview of Object-Oriented Programming (continued)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

Page 7: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 7

An Overview of Object-Oriented An Overview of Object-Oriented Programming (continued)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

Page 8: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 8

Defining Classes and Creating Class Defining Classes and Creating Class DiagramsDiagrams

• 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

Page 9: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 9

Defining Classes and Creating Class Defining Classes and Creating Class Diagrams (continued)Diagrams (continued)

• Class diagram: rectangle with 3 sections– Class name

– Attribute names and data types

– Methods

Page 10: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 10

Defining Classes and Creating Class Defining Classes and Creating Class Diagrams (continued)Diagrams (continued)

Page 11: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 11

Defining Classes and Creating Class Defining Classes and Creating Class Diagrams (continued)Diagrams (continued)

Page 12: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 12

Defining Classes and Creating Class Defining Classes and Creating Class Diagrams (continued)Diagrams (continued)

Page 13: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 13

Understanding Public and Private Understanding Public and Private AccessAccess

• 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

Page 14: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 14

Understanding Public and Private Understanding Public and Private Access (continued)Access (continued)

Page 15: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 15

Understanding Public and Private Understanding Public and Private Access (continued)Access (continued)

• In class diagrams, access type is shown as a plus sign (+) for public, and a minus sign (–) for private

Page 16: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 16

Understanding Public and Private Understanding Public and Private Access (continued)Access (continued)

Page 17: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 17

Instantiating and Using Objects 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

Page 18: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 18

Instantiating and Using Objects Instantiating and Using Objects (continued)(continued)

Page 19: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 19

Instantiating and Using Objects Instantiating and Using Objects (continued)(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

Page 20: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 20

Understanding InheritanceUnderstanding 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

Page 21: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 21

Understanding Inheritance (continued)Understanding Inheritance (continued)

Page 22: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 22

Understanding Inheritance (continued)Understanding Inheritance (continued)

Page 23: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 23

Understanding Inheritance (continued)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

Page 24: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 24

Understanding Inheritance (continued)Understanding Inheritance (continued)

Page 25: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 25

Understanding Inheritance (continued)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

Page 26: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 26

Using PolymorphismUsing 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

Page 27: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 27

Understanding Protected AccessUnderstanding 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

Page 28: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 28

Understanding Protected Access Understanding Protected Access (continued)(continued)

Page 29: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 29

Understanding Protected Access Understanding Protected Access (continued)(continued)

Page 30: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 30

Using a Predefined ClassUsing 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

Page 31: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 31

Understanding the Advantages Understanding the Advantages of OOPof 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

Page 32: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 32

SummarySummary

• 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

Page 33: An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming Concepts

An Object-Oriented Approach to Programming Logic and Design 33

Summary (continued)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