Object-Oriented Programming

Download Report

Transcript Object-Oriented Programming

Why?

OBJECT-ORIENTED PROGRAMMING

Object-oriented software development 5/1/2020 1

What am I expected to know?

 From 1250, the following items represent a partial list of things with which you are expected to be comfortable.

Please notify the instructor ASAP if you need any help

.

Syntax – braces, semicolons, naming conventions (camel casing/Pascal casing), other punctuation, and so forth Assignment statements and

arithmetic;

Decision making logic such as

if .. else

;

switch .. case

Comments – both

Class Diagrams //

and

/* … */; UML

Operators such as

=, +, -, *, /, ++, --, +=, >, <, ==, !=, >=, <=, parentheses

, and so forth Repetition (loops) such as

while, for

Methods:

calling

methods,

writing

new methods;

static

vs.

instance methods

Console input and output;

System.out

and the

Scanner class

Data types:

int

,

float

,

double

,

short

,

long

,

char

,

String

,

Boolean

, and so forth;

value

types vs.

reference

types;

Methods: parameters return values

Classes and objects; terminology such as

constructor public

, ,

getter private

, ,

static

;

return types setter

,

final

,

toString

, etc.

and ,

Creating and using objects

and their

methods

5/1/2020 2

Software Lifecycle

1.

Requirements

– what is the problem we need to solve including enough detail?

2.

3.

Specifications

– formal statement of the problem and our approach to the solution

Design

– what needs to be done to solve the problem  Iterative, starting at a

high

, somewhat

abstract

repeatedly

refining with more detail

level and

4.

5.

6.

 Continue until there is sufficient detail to implement the design

Implementation

– Coding, testing, debugging

Integration

environment (software, hardware, etc.) where it will run – integrating the solution into the

Maintenance

– Corrections, enhancements, etc. during the product’s lifetime 3 Object-oriented software development 5/1/2020

Cost

 It has been shown that the cheaper it is to fix

earlier in a life-cycle

an issue can be identified and resolved, the  If we can identify the issue before we begin designing and testing, we save on designing and testing the wrong thing, for example  If we identify the issue during testing, before shipping, we save on having to fix the problem for all customers (resulting in less customer ill-will)  If the problem is identified at the in all of the

previous N-1 stages N th

stage of the software lifecycle, we may have to make changes to fully resolve the problem  The closer to the last stage we get, the more (stages and) work is required to resolve the problem – and hence, the more costly it is 4 Object-oriented software development 5/1/2020

Object-oriented terminology

Object-oriented software development 5/1/2020 5

Object-Oriented Terms and Concepts

 Major terms and concepts to be discussed  Modularization , Encapsulation , Abstraction  Accessibility – data hiding ( private , public )  Polymorphism – understanding which of several meanings is the one intended from the context in which it is used  Inheritance – creating a new class that is a special case of an existing class (a Student is a specialized Person , a CheckingAccount is a specialized BankAccount , a BMW is a specialized Automobile , etc.) 6 Object-oriented software development 5/1/2020

Modularization, Encapsulation, Abstraction  These concepts are related  Modularization : organizing the logical components of a solution to a problem into classes nouns in the representing the Requirements/Specifications .

prominent  Example: A Marketing Company may have Customers , Products , Suppliers , SalesPeople , Managers , Accountants , etc.

 Encapsulation : putting all related components (variables and methods) into a single class so that the class is self-contained  Example: a car encapsulates its motor , transmission , steering , etc. into one unit that is self-contained and can be used as a unit  Abstraction : once a class is built, one can treat it as a new data type and use objects

how they work insid

e of that class

without having to know

 Example: we can drive a car engine or transmission without having to understand how an works Object-oriented software development 5/1/2020 7

Standardization

Standardization

– following standards for  documentation  naming (variables, files, classes, etc.)  coding style and so forth saves time for others who may not have been the original developer but who are now maintaining or upgrading the product Object-oriented software development 5/1/2020 8

Data-Hiding

Data-hiding

(

public

,

private

, etc)  Some information should be hidden for

security

,

privacy

, and similar reasons ( credit card numbers , bank account ID and password , health records , etc.)  Other information should be hidden because the user 

Does not need to know it

 I don’t have to know how a transmission works or be able to build one to drive a car  I don’t have to understand how a television set converts an electronic signal received over the cable into a picture on the screen in order to enjoy watching a game 

Should not access it

 If the user was given uncontrolled access to some feature, the user might tinker with it unnecessarily and cause widespread problems  Problems may be intentional or unintentional 9 Object-oriented software development 5/1/2020

Data Hiding

Ideally, everything is private unless user really NEEDS to touch it

 The user of the television 

Needs access

to the buttons on the remote control to change the channel , adjust the volume , turn TV off/on 

Does not need

access to the transistors , resistors , diodes , and capacitors and so forth inside the TV  Good

rule of thumb

when designing a class – make

all attributes private

and only make a

method public

if the user of your class has a need to use it directly 10 Object-oriented software development 5/1/2020

Polymorphism

Polymorphism

literally means “

many forms

” 

Polymorphism

allows the use of the

same term

to mean

different things

, but it does so in such a way that we know what is meant

unambiguously

 For example, the operations one must perform to

draw

a

rectangle

are different than the operations one must perform to

draw

a

circle

 We call both sets of operations by the same term:

Draw

 When we know the context (what we are drawing -

circle

or

rectangle

), we understand which operations are needed to

Draw

it Object-oriented software development 5/1/2020 11

Polymorphism

Method-overloading

is one type of

polymorphism

in

Java

Overloading methods

allows you to have several methods with the

same name

but with

different parameter lists

 The

signature

of the particular

overload

distinguishes it from the others with the

same name

 Reminder: a method’s

signature

consists of the

method’s name

and the

number, types,

and

ordering

of its

parameters

12 Object-oriented software development 5/1/2020

Polymorphism, continued

   Many different

classes

(

data types

) may have features with the

same names

but

different meanings

or

implementations

Length

of a

rectangle

distance for one side object may be a measure of

Length

of

sentence

may be a count of words or letters

Length

of a

lecture

boredom) may be a measure of time (or 

Length

of a

book

is a number of

pages

Polymorphism

allows us to use the

same names

for

different things

as long as the

context

makes

clear

which

meaning is intended

13 Object-oriented software development 5/1/2020

Inheritance

 As one gains experience with object-oriented programming, he or she begins to accumulate a

collection of classes

he or she has created  Once this set of classes becomes large enough,

re-use

of classes becomes possible  When one needs a class, the set of existing classes may not contain exactly what we need, but it may contain a class that is close  Rather than starting from scratch to create the class we need, we can start with the class that is close and only add the necessary features to get the one we need Object-oriented software development 5/1/2020 14

Inheritance

Inheritance specialize

allows us to take a

general case

it by adding new features

without starting over

and  If we have a

Person

class (with a name , address , ID , phone number , birth date , sex , etc.)  We can create a

Student

class by adding features like

Major

,

Class Schedule

,

GPA

, etc.

 We do not have to recreate the features the

Person

already has (

Name

,

birth date

,

sex

, …) class  The

Student

class inherits those from the

Person

class  We can derive an

Employee

features unique to employees class from

Person

by adding  We can derive

Soldier

class from

Person

features that make a

Soldier

by adding a unique type of

Person

15 Object-oriented software development 5/1/2020

Inheritance terminology

 If class

D

inherits from class

B

we may say 

B

is the

base class

and

D

is

derived

from it  

D

is a

subclass

of

B B

is a

superclass

of

D

 If

D

inherits from

B

(every

D

object

IS-A B

object) 

Every object

of type

D is-a

object of type

B

  Every

Employee

is-a

Person

Every

Student

is-a

Person

 Every

Soldier

is-a

Person

 However, the converse is not true.

Not

type

B

is of type

D

every object of   Some

Persons

are not

Employees

Some

Persons

are not

Students

16 Object-oriented software development 5/1/2020

Classes and Objects

 A

class

is a

blue-print

,

pattern,

or describing the

attributes

and

design functionality

every instance of the class of  An

object

is one unique

instance

that

blue-print

or

design

created from  Many

objects

(

class

) may be created from the same blue-print  Each object will have the

same attributes

and the

same functionality

(all cars have motors, seats, brakes, …)  The

values

of the

attributes

allow the

objects

to

differ

 Example, every

Person

object has a

name

attribute, but every person may have his/her own value for that attribute – everyone has a name, but not everyone has the same name Object-oriented software development 5/1/2020 17

Message passing

 The act of

asking an object

of a class to

perform some action

(by invoking one of its

methods

) is called

passing a message

to the object. We may also refer to this action as   

Invoking

the

method Calling

the

method Calling

a

function

 For example,   We may

send

a

message Name

(by using a public to a

Person

object asking it to

tell us its getter

method such as

getName( )

) We may

report send

its a

message current balance

to a

bank account object

asking it to (again by invoking a public

method

) 

Objects communicate

with each other by

sending messages

(asking each other to take some specific action) 18 Object-oriented software development 5/1/2020

State of an Object

 The

set of values

for

all

of the

attributes

object at a

particular point in time

of an describe its

state

 An

object’s state

other objects may

change over time send it messages

as (by invoking its

methods

) that ask it to take some action(s) that

change

the

values

of one or more of its

attributes

 A

setter

method is an example of a method designed to

change

an object’s

state

 A

constructor

method when the object is

initializes

an object’s

first created

Object-oriented software development 5/1/2020

state

19

Object-Oriented Programming in Java

 Recall: an object data and all the should encapsulate functionality all of the required for it to be self-contained for its purpose  To create a Java class that encapsulates material, what does it need to contain?

this  Attributes (also called fields or variables ) that hold the data the object needs to do its job  Methods that use to do the job or manipulate the data in the fields  An object may have a few methods or it may have many methods depending on the range of capabilities objects of the class need Object-oriented software development 5/1/2020 20

Objects in Java

 Objects contain  Attributes ( fields/variables ) private  Methods  Constructors – one or more methods, one of which is invoked to initialize the fields when an object is first created – must have same name as the class  Getters/Setters – public methods that give other objects controlled access to the private fields  Accessor Methods – methods that allow the user to retrieve some information (and/or manipulate it ) from the object without changing the values of its attributes  Manipulator (Mutator) the user to modify Methods – methods that allow the value(s) of one or more fields 21 Object-oriented software development 5/1/2020

Constructor Methods

 Constructors  Each class has one or more  Name of constructor is

same

as name of class  Purpose is to give initial values to fields when an object is created  done in any way that makes sense for the object  Example: Person kelly = new Person (“Kelly”, “Green”, 21); Object Type – Class name Object reference name Call to one of the constructors for Person 5/1/2020 22 Object-oriented software development

Constructor Methods

 Class may have 1 or more constructors  All have same name as class  None return anything ( not even void)  Each must have unique signature  The name must be same for all  The number , type , and/or order of parameters must be unique for each different constructor  Examples  Person ( ) // Default constructor – no parameters  Person (String first, String last, int age) // Parameterized 23 Object-oriented software development 5/1/2020

Constructors in UML (astah*)

Private Attributes/Fields Two constructor methods 5/1/2020 24 Object-oriented software development

Getter Methods

 A getter is a public method that retrieves the value of a private attribute ( field )  Allows user of object to retrieve a value without changing it; examples:  A student can see, but not change, his/her

GPA

 A bank’s depositor, can determine his/her

balance

(without making a deposit or withdrawal) but cannot change it  An employee can “see” his/her

salary

, but cannot change it  A private field may or may not have its own public getter  If the user

SHOULD

be able to see the value of the corresponding attribute, it should have a getter (all of the examples above)  If a field should be hidden from the user and should be only for internal use by the object, it

SHOULD NOT

have a getter (the user of a TV need not be able to get information such as the voltage across a certain circuit or the electrical resistance of a particular resistor)  A person would not want a public getter for his/her credit card number 25 Object-oriented software development 5/1/2020

Getters in UML

Object-oriented software development

Getter

methods for the private fields 5/1/2020 26

Setter methods

 A Setter method is a public method user to change the value of a that allows a private attribute in a way controlled by the designer of the class  A field

should have

a setter to change an attribute if the user should be allowed  A remote control may use a setter to change (set) the channel on a TV  A brake may be used to change the speed of a car  A field

should not have

allowed a setter if the user to change its value. Examples: should not be  Bank balance  GPA  Salary 27 Object-oriented software development 5/1/2020

Setters

 A setter method usually takes one or more parameters used to returns nothing set the new field value but it - it just changes the field value Object-oriented software development 5/1/2020 28

Accessor Method

 An accessor method

does not change

is a public method that any field in the object but it allows one to use the values of those fields for some purpose  It may use the values in the payRate and hoursWorked fields (attributes) to calculate and return a weekly salary  It may format the values of some of the fields for display by the driver program  It may calculate GPA from credits and hours earned  Each examples uses information without changing it 29 Object-oriented software development 5/1/2020

Accessor Method in UML

Object-oriented software development Formats the name and age for display by the driver program but it does not change those fields – probably should be named toString ( ) 5/1/2020 30

Manipulator (Mutator) Method

 A manipulator (mutator) method may change the value of one or more fields in some way controlled by the designer of the class  In a Circle object, a manipulator/mutator may set the value of an area field formula involving the with a calculation radius that uses a (

area = p * radius 2

)  In an Employee object, a mutator may calculate the value of the SSN tax field fields in the object using the values of other Object-oriented software development 5/1/2020 31

Java code …

Three attributes/fields Object-oriented software development 5/1/2020 32

Using an object

How many fields does the driver class have?

How many class names are used here?

How many objects can you see?

What constructors are used?

What methods of the Person class are used?

What getters/setters are used?

Object-oriented software development What mutator is used?

What accessors are used?

5/1/2020 33