chapter 10 introduction to objects and classess 1

18
Chapter 10 Introduction to Objects and Classess 1

Upload: rose-hancock

Post on 14-Dec-2015

232 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 10 Introduction to Objects and Classess 1

Chapter 10 Introduction to Objects and Classess

1

Page 2: Chapter 10 Introduction to Objects and Classess 1

How can one design a program?

Top-down structured design: uses algorithmic decomposition where each module denotes a major step in some overall process

Object-oriented design: divides the problem into a set of objects that interacts to solve the problem. Your program’s properties and behaviors are modelled based upon real objects like cars, books, houses, etc.

2

Page 3: Chapter 10 Introduction to Objects and Classess 1

Why OOD?

Software is complex (too many people is doing too many things – the mess is inevitable )

One of the main goals in programming is to avoid the redundancy and objects can help to do this (inheritance)

Objects can help increase modularity through data hiding (encapsulation)

3

Page 4: Chapter 10 Introduction to Objects and Classess 1

OK, but what is object ?

An object is a thing, either tangible or intangible.

An object-oriented program consists of many objects.

An object is composed of identity, state (attributes, data, and their current values) and behavior (operations) .

4

Page 5: Chapter 10 Introduction to Objects and Classess 1

Identity, State, Behavior

Identity is the property of an object that distinguishes it from all other objects.

The failure to recognize the difference between the name of the object and the object itself is the source of many errors in object-oriented (OO) programming.

5

Page 6: Chapter 10 Introduction to Objects and Classess 1

Identity, State, Behavior

The state of an object encompasses all of the (static) properties of the object plus the current (dynamic) values of each of these properties

A property is an inherent or distinctive characteristic, trait, quality, or feature that contribute to making an object uniquely that object

We will use the word attribute, or data member, to refer to the state of an object

6

Page 7: Chapter 10 Introduction to Objects and Classess 1

Examples of State

Properties

Elevators travel up or down

Vending machines accept coins

Clocks indicate the current time

Values

Current floor

Number of coins deposited

The number of minutes since the last hour

7

Page 8: Chapter 10 Introduction to Objects and Classess 1

Identity, State, Behavior

Behavior is how an object acts and reacts, in terms of state changes and interactions with other objects.

An operation is some action that one object performs upon another in order to elicit a reaction.

We will use the word method to describe object behavior in java.

Invoking a method causes the behavior to take place.

8

Page 9: Chapter 10 Introduction to Objects and Classess 1

Classes

Classes are the definitions (or blueprints) used to create objects. I’d say: descriptions of objects.

To make a car the manufacturer must first have a design from which to build the first car. Then, once all the problems are worked out, the design is used to build all the cars of that model.

9

Page 10: Chapter 10 Introduction to Objects and Classess 1

Objects

An object is an instance of a class.

If we have a class definition called Car, then we can think of Audi, BMW, and Corvette as each being an instance (object) of the class Car, i.e., they are each a type of car.

10

Page 11: Chapter 10 Introduction to Objects and Classess 1

Object example 11

Audi 6 BMW Z3 Corvette

• Notice that all objects are of the same type. All objects are cars!

Car Car Car

Page 12: Chapter 10 Introduction to Objects and Classess 1

Classes and Objects

An object is an instance of exactly one class!!!

Corvette can not be an instance of a car class and an instance of a plane class at the same time.

An instance of a class, an object, belongs to that particular class.

A Corvette is a car Corvette belongs to the class Car.

12

Page 13: Chapter 10 Introduction to Objects and Classess 1

Classes

Once a class is defined you can create as many instances of the class (objects from the class) as you would like.

Once a blue print is completed for the 2003 Porsche 911, Porsche will use an assembly line to build as many instances of the 2003 Porsche 911 as they wish.

13

Page 14: Chapter 10 Introduction to Objects and Classess 1

Class car and objects- graphically 14

Audi 6 BMW Z3 Corvette

Car Car Car

CarThis line shows an instance-of relationship.

Page 15: Chapter 10 Introduction to Objects and Classess 1

Defining a class

Properties are variables which describe the essential characteristics of an object.

• Properties of a car: color, model, make, how many doors, transmission type, direction of movement, etc.

Behaviors are methods that describe how the object behaves and how the properties may be modified.

• Behavior of a car: braking, changing gears, opening doors, moving forwards or backwards, etc.

15

Page 16: Chapter 10 Introduction to Objects and Classess 1

Classes

A class is a collection of fields (data) and methods (procedure or function) that operate on that data .

16

Circle

centreradius

circumference()area()

Page 17: Chapter 10 Introduction to Objects and Classess 1

Adding Fields: Class Circle with fields Add fields

The fields (data) are also called the varaibles.

17

public class Circle { public double x, y; // centre coordinate public double r; // radius of the circle

}

Page 18: Chapter 10 Introduction to Objects and Classess 1

Data Abstraction

Declare the Circle class, have created a new data type – Data Abstraction

Can define variables (objects) of that type:

Circle aCircle;

aCircle = new Circle();

OR

Circle bCircle = new Circle();

18