This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1Vladimir Misic: vm(at)cs.rit.edu
Object-Oriented Programming Basics
Reading for this week†:
• BG 1-4.10
• UNIX 1,2,4,9
• JAVA 0,1
†BG = Beginner’s Guide
UNIX = Harley Hahn’s Student guide to Unix, Harley Hahn
JAVA = An Introduction to Object Oriented Programming with Java, Thomas Wu
2Vladimir Misic: vm(at)cs.rit.edu
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.
3Vladimir Misic: vm(at)cs.rit.edu
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)
4Vladimir Misic: vm(at)cs.rit.edu
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) .
• Identity? State? Behavior? Too many new expressions – I’ll get a headache!!!
5Vladimir Misic: vm(at)cs.rit.edu
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.
6Vladimir Misic: vm(at)cs.rit.edu
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
7Vladimir Misic: vm(at)cs.rit.edu
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
8Vladimir Misic: vm(at)cs.rit.edu
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.
9Vladimir Misic: vm(at)cs.rit.edu
Objects and Classes – graphical representation
• A graphical representation of an object:
< Object name >
• A graphical representation of a class:
< Class name >
10Vladimir Misic: vm(at)cs.rit.edu
Object example
Audi 6 BMW Z3 Corvette
• Notice that all objects are of the same type. All objects are cars!
11Vladimir Misic: vm(at)cs.rit.edu
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.
12Vladimir Misic: vm(at)cs.rit.edu
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.
13Vladimir Misic: vm(at)cs.rit.edu
Object example
Audi 6 BMW Z3 Corvette
• Notice that all objects are of the same type. All objects are cars!
Car Car Car
14Vladimir Misic: vm(at)cs.rit.edu
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.
15Vladimir Misic: vm(at)cs.rit.edu
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.
16Vladimir Misic: vm(at)cs.rit.edu
Class car and objects- graphically
Audi 6 BMW Z3 Corvette
Car Car Car
CarThis line shows an
instance-of relationship.
17Vladimir Misic: vm(at)cs.rit.edu
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.
18Vladimir Misic: vm(at)cs.rit.edu
Defining a class
Behavior or Method 1
Behavior Behavior or or
Method 4 Method 2
Behavior or Method 3
Properties
or
Instance Variables
19Vladimir Misic: vm(at)cs.rit.edu
Instance variables
• The class definition will include parameter definitions (properties) that represent data about a particular object, instance variables.
• Example, Jessica's car may have 4 gallons of gas in it while Clint's car has 10 gallons.
• The amount of gas in each car may change without affecting the amount of gas in the any other cars.
• All instances (objects) of a class will have a set of instance variables that are specific to that individual object.
• The combination of the values of these instance variables is known as the object’s state.
20Vladimir Misic: vm(at)cs.rit.edu
Instance variables
Audi 6 BMW Z3 Corvette
Car Car Car
Car
MaxSpeed = 155 MaxSpeed = 165 MaxSpeed = 145
MaxSpeed
21Vladimir Misic: vm(at)cs.rit.edu
Class variables
• The class definitions may also include parameter definitions that represent data that is shared by all class instances (objects), called class variables.
• In the case of the car class, we will define a maximum allowed speed, by the law (variable LMaxSpeed). This will be the same for each individual car (that’s why your car have those annoying speed limiters ).
22Vladimir Misic: vm(at)cs.rit.edu
Class variables
Audi 6 BMW Z3 Corvette
Car Car Car
Car
MaxSpeed = 155 MaxSpeed = 165 MaxSpeed = 145
MaxSpeed
LMaxSpeed=155
23Vladimir Misic: vm(at)cs.rit.edu
Class variables
• Class variables may also be used to keep track of things such as how many instances of a class exist.
• Example: let’s create a counter the records how many cars are in the garage.
24Vladimir Misic: vm(at)cs.rit.edu
Class variables
Audi 6 BMW Z3 Corvette
Car Car Car
Car
MaxSpeed = 155 MaxSpeed = 165 MaxSpeed = 145
MaxSpeed
LMaxSpeed=155
NumCars = 3
25Vladimir Misic: vm(at)cs.rit.edu
Constant parameters
• If it was variables instead of parameters, that would be oxymoron.
• If there is a parameter whose value should not change while your program is running, that parameter type is called a constant.
• The LMaxSpeed parameter that we defined for the Car class is a constant. The maximal speed is limited by the low, and it cannot be changed once the car has been built.
• What about Z3 and 165 mph? Well, the owner has just made an illegal modification !!!
26Vladimir Misic: vm(at)cs.rit.edu
Messages
For Objects
• The object to whom the message is being sent.
• The name of the method (behavior) that object is to execute.
• Any parameters (variables) needed by that method.
For Humans
• Who the message is for.
• What we want the person to do.
• What information is needed to do it.
Audi 6 • turnOnHazard()
27Vladimir Misic: vm(at)cs.rit.edu
Messages and Methods
• In order to process a message, an object needs to have a method defined for the requested task.
• A method is a small, well-defined piece of code that completes a specific task.
• For our previous example, we need to define a method to turn on the car's hazard lights.
28Vladimir Misic: vm(at)cs.rit.edu
Messages and Methods
Audi 6 BMW Z3 Corvette
Car Car Car
Car
MaxSpeed = 155
turnOnHazard()
MaxSpeed = 165
turnOnHazard()
MaxSpeed = 145
turnOnHazard()
MaxSpeed
LMaxSpeed=155
NumCars = 3
turnOnHazard()
29Vladimir Misic: vm(at)cs.rit.edu
Instance methods
• Each class can have methods that are specific to each object, called instance methods.
• These can only affect that object's parameters, i.e., it’s instance variables.
• Example: If BMW has 4 gallons of gas and someone puts 6 more gallons of gas in his/her car, the car now has 10 gallons. The amount of gas in Audi and Corvette is unchanged.
• It is also possible that you want information from an object; in this case, you would define a method that sends (returns) a message back to the requester containing that information.
• We need to know how much gas is in our cars, so we will create a new method that returns the value of GasLevel variable for our car.
• Modifier (sometimes called a mutator)– Changes the value associated with an attribute of the object (or class)
• Accessor– Returns the value associated with an attribute of the object (or class)
• Constructor– Called once when the object is created (before any other method will be
invoked)
• Destructor– Called when the object is destroyed
36Vladimir Misic: vm(at)cs.rit.edu
OOP
• When writing object-oriented programs, first one must define the classes (like Car).
• Then, while the program is running, the instances of the classes (objects) (such as Audi, BMW, Corvette in our example) are created.
37Vladimir Misic: vm(at)cs.rit.edu
OOP
• Object-oriented programming allows the programmer to hide the implementation details from the other objects and the users.
• In other words the implementation is transparent to the other objects or the user.
• Example: Although our computers all are different “under the hood”, we don’t need to know what’s there to be able to use them.
38Vladimir Misic: vm(at)cs.rit.edu
OOP - benefits
• An object can be written and maintained separately from the rest of the program, modularity.
• An object has a “public face” that it uses to communicate with other objects, but other objects can not directly access its instance variables, information hiding.
39Vladimir Misic: vm(at)cs.rit.edu
Inheritance
• All classes in Java are organized into a class hierarchy.
• The highest level classes are very general and the lower level classes are more specific.
• The lower level classes are based upon the higher level classes and inherit instance variables and methods from those higher level class. They also may contain their own (new) instance variables and methods beyond the higher level class definition.
40Vladimir Misic: vm(at)cs.rit.edu
Inheritance
• A higher level class is called a superclass; a lower level class is called a subclass.
• A subclass may also be a superclass
• Inheritance allows you to define certain behaviors once and then to reuse those behaviors over and over again in the subclasses. This is called reusability.
41Vladimir Misic: vm(at)cs.rit.edu
Inheritance
• Our Car class is very general.
• Let's define a new class called BMW that contains the parameters: model, color, engine size.
• When a double is stored to a float, part of the number is lost. In this situation, the seven most significant digits are preserved and the rest of the information is lost.
52Vladimir Misic: vm(at)cs.rit.edu
Char
• Character values may represent both printable and non-printable characters:– Printable: ‘a’, ‘&”, …
– Non-printable: ‘\n’ new line, ‘\t’ tab, …
• Unicode 16b support for every character in the world: ‘\u0000’ to ‘\uFFFF’