0% found this document useful (0 votes)
49 views

07 Object Oriented Concepts

1) Models are simplified descriptions that are used to make decisions, make predictions, and simulate processes. They reduce complexity by only considering relevant data. 2) Object-oriented programs use objects to model the problem domain. Objects are constructed from data and code that represent elements of the model. 3) An object contains both data (attributes) and behaviors (methods) and encapsulates them to manage its own data. This protects data from misuse by other parts of the program.

Uploaded by

urbedglvbh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
49 views

07 Object Oriented Concepts

1) Models are simplified descriptions that are used to make decisions, make predictions, and simulate processes. They reduce complexity by only considering relevant data. 2) Object-oriented programs use objects to model the problem domain. Objects are constructed from data and code that represent elements of the model. 3) An object contains both data (attributes) and behaviors (methods) and encapsulates them to manage its own data. This protects data from misuse by other parts of the program.

Uploaded by

urbedglvbh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 10

Modeling Information

Object-Oriented Concepts ๏ Model: simplified description


๏ Used to make decisions,
CSE 114: Computer Science I make predictions, and
Stony Brook University simulate processes
๏ Models are an abstraction
๏ They reduce complexity by
only considering relevant data

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

๏ Objects track information about some part of the ๏ Implementing objects


problem ๏ Advanced concepts: access modifiers and static
What is an Object, Anyway?
Object Queries
๏ Objects are a very natural way to view the world around us
๏ Query: question to which an object can respond
๏ Ex. People, things, even non-physical stuff (i.e., a bank account)
๏ Queries are always directed to a specific object, and
๏ Objects are defined in terms of attributes and behaviors answered by that object
๏ Attribute: properties that an object has (e.g., eye color, height,
weight) ๏ Answers are based on the object’s attributes

๏ 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)

Objects Are Self-Managing Procedural Programming


๏ Most older programs use a Function 1 Function 2

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?

Classes and Objects


๏ Attributes ๏ Every object is defined by a class
๏ Employee name, Social Security #, date of birth, date of ๏ A class is a template for objects of that type — it
hire, pay rate describes the types of data and the behaviors that each
object of that type has

๏ Behaviors ๏ If two objects have identical behaviors and attribute


types, they belong to the same class
๏ Set/get each of the values above, calculate pay for a given
number of hours ๏ A specific object is called an instance of its class
๏ e.g., you and your neighbor are instances of the Student
class (same kinds of data, different actual data)
The Anatomy of a Class
Classes and Instances
๏ A class contains two types of elements:
๏ Instance data — variables representing an object’s
attributes
๏ Instance methods — methods (code) that define an
object’s behaviors
๏ Each class is defined using the Java keyword “class”
๏ Curly braces enclose the data and method
Each ball has has the same shape, but a unique size and color declarations that make up the class

Class Definition Example


class Example // defines a new data type called “Example”

{

instance variable declarations go here...
 Object Methods

instance method definitions go here...
 ๏ Besides data, objects contain methods — blocks of code that
} operate on that data
๏ Normally, every class is defined in a separate source file named ๏ e.g., a Point class might have methods to change the
for the class values of its x and y coordinates
๏ If this is done, the keyword “public” must be written before ๏ Methods provide a way for objects to communicate with one
“class” another
๏ If several classes are defined in the same file, exactly one must ๏ Methods must be defined inside a class
be declared “public” and share the name of the file.
Access Modifiers
General Access Guidelines
๏ A class may restrict access to its methods and variables
๏ You should always make the variables in your objects
๏ The keyword public means that something is freely private by default
accessible inside and outside the class
๏ This prevents unauthorized modifications
๏ The keyword private means that a variable or method can
only be seen or used by methods inside the class ๏ Methods should be public only if other classes may
๏ If no access modifier is used, the variable or method can only have a good reason to call them; otherwise, they
be seen/used by classes in the same package should be declared private
๏ A package is a group of related files ๏ Private methods may only be used within their class

Public Methods Public Methods

Method Method
Designing Classes
Method Method Data

Hidden Method ๏ To design an object, you need to determine:


Data Method Method
๏ What data it needs to represent
Hidden Methods ๏ What behaviors/methods it should have
Method
Objects communicate ๏ What other objects it may work with
with one another via
Method ๏ One way to do this is via CRC cards
their public methods
Introduction to CRC Cards

๏ A CRC (class, responsibilities, and collaborators) card Robot


represents and summarizes a single class or object type
Street (X coordinate) World
Avenue (Y coordinate) Beeper
๏ Each card contains a class name, its responsibilities (on Direction faced
the left), and the helper classes it needs (on the right) # of beepers held

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

Another Style of Summary


Robot Class Name

- 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

+ = “publicly-accessible” ๏ These messages tell the object to do various things


- = “not publicly-accessible” ๏ Every object has its own set of possible actions
Primitive and Reference Data
Creating An Object ๏ Primitive data has a known memory size (based on its type)

๏ 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

Primitives vs. References


๏ Consider the following declarations:
int num;
num
String name;
๏ Primitive variables hold an actual value
name
๏ An object variable only holds the address of an
object!
๏ No String object actually exists yet
๏ Initially, name is null (it doesn’t point to anything)
Object Initialization
num = 42; num 42

name = new String(“John Smith”);


• The keyword “new” instantiates (creates) a new name “John Smith”
object in memory
• “new” returns the memory address of the new
object
• An object is an instance of a class

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

Constructors More on Constructors


๏ A constructor is a special method that is used to • A constructor has the same name as the class
instantiate (“construct”) a new object
• A constructor has NO return type
๏ Constructors have the same name as the class
• If (and only if) no constructors are defined, Java
๏ Constructors set the initial values of the object’s provides a class with a default constructor
attributes
• This constructor takes no parameters and does not
๏ These values may be predefined in the constructor or assign any specific values to the object’s variables
passed in as arguments when the object is instantiated
• A class may have multiple constructors
Working With Class Members
Instance vs. Class Members
๏ To declare that a variable or method is shared by all of the
๏ By default, variables and methods inside a class are instance instances of a class, use the keyword static
variables/methods
๏ A static variable only exists once for all instances of a class
๏ They exist independently for every instance of that class — its value is shared

๏ 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)

You might also like