1 Introduction to Object Orientation (OO) & Related Terminology In a non-programming specific way, orient ourselves to the principles and related terminology to OO development Identify KEY OO Terms
Mar 26, 2015
1
Introduction to Object Orientation (OO) &
Related Terminology
In a non-programming specific way, orient ourselves to the principles and related terminology to OO development
Identify KEY OO Terms
2
GOALS...
TO understand prevailing methodology & related principles
To match relevant programming terminology to OO principles
3
Development in VB:
Given a problemStarted CodingAsked Questions on What
you were Supposed to do !!!Made Major changes to your
programsSome testing...
4
...Development in VB:
Asked for me to Grade itWhite Elephant(Major) modificationsFinally Done !!!
5
...Development in VB:
Then, the Next project followed the same paradigm with little reuse of prior code
6
VB Terms:
Variables
Events
Subroutines
7
Development in Java:
Remember how I required you to code the File I/O projects ?
Structures Separate code module
routines to open, read and write File I/O
Display data on a form To/From Structures
8
Development in Java:
In General, we separated the Display (GUI) from the Data Gathering from the Data
Modifications
9
Development in Java:
Furthermore, In the Code Modules we attempted to Separate the processing from the Displaying as the Code Modules should make NO reference to the controls on the form
(textboxes, labels, etc)
10
Development in Java:
In theory, we could have taken your Code module and used it for other programs without:
Knowing or Caring how the data was processed, modified or Displayed
11
Now in Java we WILL…
Given a problem
UNDERSTAND the problem
Break the problem up into manageable pieces
12
Now in Java we WILL…
Identify the relevant functions that each piece needs to perform
Create the variables necessary to maintain the STATE of each “piece”
13
Now in Java we WILL…
Identify how each “piece” will communicate
Develop the main program driver that will control the process (except for event drivenProjects)
14
Now in Java we WILL…
Test, debug and modify
Asked for me to Grade it
Done !!!
15
Java Terms:
Attributes
Events
Methods
16
In our File I/O project, we can write the following methods:
Open a file Process a file Update a file Close a file
17
In our File I/O project, we can write the following methods:
Read data from the user (console)
Send data to the user (console)
These methods will be able to be reused for any other project that requires Retrieving / Sending data
18
Old School Development:
Set up separate phone lines FOR EACH person you wish to talk to
Wasteful
Repeats 99 percent of the same functionality
19
OO Development:
Develop classes that handle the different parts of a GENERIC phone communication process
(identify who to call, place a call, engage in conversation, end the call)
20
OO Development:
Set up 1 general use phone line that can handle multiple INSTANCES of phone calls to anyone who has a phone
Creates useful and extensible components
21
OO Development:
Reuses a generic communication protocol and instantiates it with a STATE based on the person you wish to talk to (phone number, etc)
22
TPS: PERSON
Think of a generic “person” blueprint and answer the following questions:
23
What ATTRIBUTES (properties) apply to the Person Blueprint ?
24
What ATTRIBUTES (properties) apply to the Person Blueprint ?
HairVoiceSizeSkin ToneGenderRaceSSNAGE
25
What BEHAVIORS (methods) apply to the Person Blueprint ?
26
What BEHAVIORS (methods) apply to the Person Blueprint ?
Change Hair ColorChange SizeChange Gender
27
What if we disallow or encapsulate the ability for ANY user of the Person Blueprint from altering hair color ?
28
How can we use this blueprint to create other types of “Persons” ?
Teacher Person
Policeman Person
Male Person
Female Person
29
How does the use of the Generic Person Blueprint assist us in creating these “instances” of the generic Person ?
30
What about Attributes and Behaviors that are different in these instances ?
31
Answers the SAME questions for:
A Generic Car Blueprint
32
Answers the SAME questions for:
A Salary Calculation Application
33
OOP
34
We will use the LOAN class analogy
There can be many types of loans: car mortgage student
35
OOP in a Nutshell:
A program models a world of interacting objects
Objects create other objects and “send messages” to each other (in Java, call each other’s methods)
Each object belongs to a class; a class defines properties of its objects
A class implements an ADT; the data type of an object is its class
Programmers write classes (and reuse existing classes)
36
Abstraction
... relevant to the given project (with an eye to future reuse in similar projects).
Abstraction means ignoring irrelevant features, properties, or functions and emphasizing the relevant ones...
“Relevant” to what?
37
Example:
In identifying the relevant elements in building a generic blueprint for handling various loans, you can ignore adding functionality that calculates the square of all natural numbers
38
Encapsulation
Encapsulation means that all data members (fields) of a class are declared private. Some methods may be private, too.
The class interacts with other classes (called the clients of this class) only through the class’s constructors and public methods. Constructors and public methods of a class serve as the interface to class’s clients.
39
Encapsulation
The Hiding or Shielding of some of the properties and or behaviors of the class from users of the class
40
Encapsulation ensures that structural changes remain local
Changes in the code create software maintenance problems
Usually, the structure of a class (as defined by its fields) changes more often than the class’s constructors and methods
Encapsulation ensures that when fields change, no changes are needed in other classes (a principle known as “locality”)
41
Example:
In building the loan class, you would hide an attribute that holds the Prime Interest Rate as well as any intermediate calculation attributes
42
Example:
Encapsulation allows the class to restrict how the attributes can be modified
43
True or False? Abstraction and encapsulation are helpful for the following:
Team development ________
Reusable software ________
GUI programming ________
Easier program maintenance ________
44
Answer:
Team development ________
Reusable software ________
GUI programming ________
Easier program maintenance ________
T
T
T
(True if you are working on system packages, such as Swing)
F
45
Inheritance
A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding its own.
Inheritance represents the is a relationship between data types. For example: a HomeLoan is a ;loan.
46
Inheritance
The Use of a BASE or Blueprint class that contains the common properties and behaviors to create a subset of the class
47
Example:
With our loan class, the calculation of the Interest Rate may be the same Regardless of the type of loan. So, you could design the Loan class so that...
48
Example:
… It does the calculation and all of the loan types can inherit and USE thatCalculation.
49
True or False? Inheritance is helpful for the following:
Team development ________
Reusable software ________
GUI programming ________
Easier program maintenance ________
50
Answer:
Team development ________
Reusable software ________
GUI programming ________
Easier program maintenance ________
F
T
???
T
51
Polymorphism
Polymorphism ensures that the appropriate method is called for an object of a specific type when the object is disguised as a more general type.
Good news: polymorphism is already supported in Java — all you have to do is use it properly.
52
Polymorphism
The Use of a base class to serve as an interface to the use or implementation of ANY Derived Classes
53
Called Dymanic Method Binding or Late Binding
You can create a generic blueprint class that performs methods commonto any class that inherits from it.
You only need to create a placeholder of the generic class and then fill it in as needed.
54
Example:
With the car loan, we create the generic Loan class.
This Class performs the common methods and holds common attribute STATE
55
Example:
We can create a placeholder of the Loan class and then fill it with the studentloan inherited version of the Loan class
And it will , by default, contain the same public methods as the Generic Loan class
56
TPS:
Take one of the examples of Person, Car or Salary Calculation to create a SUPER BLUEPRINT that maintains attributes and behaviors that ARE COMMON regardless of the TYPE of Person, Car or Type of Salary being calculated (inheritance)
57
TPS:
Using the previous example, what attributes or behaviors can be
Hidden ?
58
Other Characteristics of OO
Divide and Conquer Principle
Interface Principle
Information Hiding Principle
Generality Principle
59
Terms:
Inheritance
Encapsulation
Polymorphism
60
Terms:
Class
Super Class
Sub / Inherited / Derrived Class
61
Terms:
Interface
Abstract Class
62
Terms:
Public
Private
Protected
63
Terms:
Attributes
Methods
Extends
64
Terms:
Override
Object
65
TEST IS NEXT !!!