07 Object Oriented Concepts
07 Object Oriented Concepts
07 Object Oriented Concepts
Object-Oriented Programming
Models and Software
๏ An object-oriented program uses objects to model the
๏ Models are used to maintain information and problem domain (the things we have to work with)
answer queries about something
๏ Objects are constructed from data and program code
๏ We can implement models using software
๏ Topics to consider:
๏ An object is a block of code that represents one
element of a model ๏ Designing objects
๏ Behavior: actions that an object can perform (e.g., walk, speak, pick ๏ Answers can take any form: T/F, integer, etc.
up)
๏ An object can only respond to pre-programmed
๏ An object is an entity that contains both data and behavior
queries (methods/messages)
procedural methodology
๏ An object contains all of the functions (methods, representing
“behavior”) that need to operate on its data (“attributes”) ๏ Program code is divided into distinct
functions or procedures
๏ This is called encapsulation, which means that an object is
๏ Data is stored separately, and passes
responsible for managing its own data through these procedures Data
๏ An object may hide its contents from the rest of the program ๏ Data may be stored globally
๏ This protects our data from mischief and can conceal how we ๏ This means that data is uncontrolled
represent and manipulate data inside an object and unpredictable — there is no
way to protect data from other parts
of the program Function 3 Function 4
Objects Are Safer Than Global Data Storage Object-Oriented Programming Example
๏ Objects are the building blocks of an OO program
๏ In an OO program, data is distributed among objects; there is
no central “pile” of program data ๏ An OO program is a collection of interacting objects
๏ An object contains all of the functions (methods, representing ๏ Example: Employee management software
“behavior”) that need to operate on its data (“attributes”)
๏ This system must maintain information about
๏ This is called encapsulation, which means that an object is employees
responsible for managing its own data
๏ Each employee will be represented by a unique object
๏ An object may hide its contents from the rest of the program
๏ What kinds of data should these objects store?
๏ This protects our data from mischief and can conceal how we
represent and manipulate data inside an object ๏ What kinds of behaviors should these objects provide?
Method Method
Designing Classes
Method Method Data
Move forward
๏ You can use index cards to do this, or just make notes Turn left
Pick up beeper
on paper Put down beeper
Source: http://www.agilemodeling.com/artifacts/crcModel.htm
- name: String
- modelNumber: int
- currentDirection: char
- currentXCoordinate: int
Attributes
Format: name : type
Working With Objects
- currentYCoordinate: int
- itemsHeld : int
๏ Before we can use an object in our program, we must
create a new instance of that object
+ getCurrentDirection () : char
+ turnLeft () : void
+ moveForward (distance : int) : void Behaviors ๏ We need to create a variable that represents that
+ pickUp () : void
+ drop () : void
Format: name ( input ) : return_type object
๏ After we create the object, we can send messages to
๏ A UML class diagram provides a summary of the attributes and
behaviors for a given class it
๏ To create (instantiate) an object: ๏ This memory can be requested when the program loads
๏ For other information, we may not know its exact size at
๏ Declare a new variable to hold the object
launch time, or its size may change as the program
๏ Use the keyword new to create the object: executes
MyClass foo = new MyClass( ); ๏ Memory for this data must be allocated on the fly
๏ “new” invokes an object’s constructor ๏ We need two things to store and use this data:
๏ A constructor is a special behavior (method) that ๏ A block of RAM for the actual data
creates a new object of a specific type
๏ A pointer (reference) to that block of memory
๏ It has the same name as the type
Object Assignment
name1 “Ada Lovelace”
๏ Remember that an object variable stores an
address
๏ If we assign one object to another, they both name2
point to the same memory address
String name1 = “Ada Lovelace”;
String name2 = “Grace Hopper”;
name2 = name1;
Speaking To Objects
Object References ๏ A message tells an object to do something
• Object variables are references to values ๏ A message consists of the object’s name, followed by a
• We can only access an object if we have a reference to “dot” (period), followed by the behavior name and
it argument list
• When all references to an object are lost, we can no ๏ Messages are terminated by a semicolon
longer access it
myRobot.move();
• Java automatically garbage collects (reclaims) Reference to
Argument list
unreferenced objects the object Behavior to
execute
๏ This also means that they cannot be used without first ๏ Static methods can only call other static methods or
creating an instance of that class variables; they cannot use instance methods/variables
๏ We can also create class variables/methods ๏ To refer to a static member, add the class name:
๏ They can be used without first instantiating a class ๏ e.g., System.in (in is a static part of the System class)