Object-Oriented programming 1 SSD3: Object-Oriented Programming and Design.
An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming...
-
Upload
gwendoline-hines -
Category
Documents
-
view
232 -
download
0
Transcript of An Object-Oriented Approach to Programming Logic and Design Chapter 2 Object-Oriented Programming...
An Object-Oriented Approach to An Object-Oriented Approach to
Programming Logic and DesignProgramming Logic and Design
Chapter 2Object-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
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
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
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
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
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
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
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
An Object-Oriented Approach to Programming Logic and Design 10
Defining Classes and Creating Class Defining Classes and Creating Class Diagrams (continued)Diagrams (continued)
An Object-Oriented Approach to Programming Logic and Design 11
Defining Classes and Creating Class Defining Classes and Creating Class Diagrams (continued)Diagrams (continued)
An Object-Oriented Approach to Programming Logic and Design 12
Defining Classes and Creating Class Defining Classes and Creating Class Diagrams (continued)Diagrams (continued)
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
An Object-Oriented Approach to Programming Logic and Design 14
Understanding Public and Private Understanding Public and Private Access (continued)Access (continued)
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
An Object-Oriented Approach to Programming Logic and Design 16
Understanding Public and Private Understanding Public and Private Access (continued)Access (continued)
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
An Object-Oriented Approach to Programming Logic and Design 18
Instantiating and Using Objects Instantiating and Using Objects (continued)(continued)
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
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
An Object-Oriented Approach to Programming Logic and Design 21
Understanding Inheritance (continued)Understanding Inheritance (continued)
An Object-Oriented Approach to Programming Logic and Design 22
Understanding Inheritance (continued)Understanding Inheritance (continued)
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
An Object-Oriented Approach to Programming Logic and Design 24
Understanding Inheritance (continued)Understanding Inheritance (continued)
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
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
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
An Object-Oriented Approach to Programming Logic and Design 28
Understanding Protected Access Understanding Protected Access (continued)(continued)
An Object-Oriented Approach to Programming Logic and Design 29
Understanding Protected Access Understanding Protected Access (continued)(continued)
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
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
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
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