Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10 Introduction to Objects and Classess 1.

Similar presentations


Presentation on theme: "Chapter 10 Introduction to Objects and Classess 1."— Presentation transcript:

1 Chapter 10 Introduction to Objects and Classess 1

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 Object example 11 Audi 6BMW Z3Corvette Notice that all objects are of the same type. All objects are cars! Car

12 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

13 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

14 Class car and objects- graphically 14 Audi 6BMW Z3Corvette Car This line shows an instance-of relationship.

15 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

16 Classes  A class is a collection of fields (data) and methods (procedure or function) that operate on that data. 16 Circle centre radius circumference() area()

17 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 }

18 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


Download ppt "Chapter 10 Introduction to Objects and Classess 1."

Similar presentations


Ads by Google