Classes and Object-Oriented Development PP

Download Report

Transcript Classes and Object-Oriented Development PP

Chapter 8
Objects
 A variable of a data type that is a class. Also called
an instance of a class.
 Stores data
 Can perform actions and provide communication
 State of object refers to the data it stores
 Behavior of object refers to the action and
communication it provides
Slide 1
© 2007 Lawrenceville Press
Chapter 8
Class
 An abstract data type
 Used to create, or instantiate, objects
 Provides encapsulation, also called information hiding
Slide 2
© 2007 Lawrenceville Press
Chapter 8
The Circle Class
Slide 3
© 2007 Lawrenceville Press
Chapter 8
A Class
public class Circle { body
private static final double PI = 3.14;
private double radius;
public Circle() {
radius = 1;
}
variables
constructor
public void setRadius(double newRadius) {
radius = newRadius;
}
Slide 4
method
© 2007 Lawrenceville Press
Chapter 8
Methods in a Class
 An accessor method is used to determine the value
of a variable
 A modifier method is used to change the value of a
variable
 A helper method is called from within a class by
other methods
Slide 5
© 2007 Lawrenceville Press
Chapter 8
Overloading Constructors
 Constructors can be overloaded to provide more
options for instantiating an object
 The compiler uses the number and types of
parameters to determine which constructor to
execute
Slide 6
© 2007 Lawrenceville Press
Chapter 8
Instance and Class Variables
 Each object of a class has its own copy of the
instance variables in the class.
 A class variable is declared with the keyword static
and only one copy of a class variable is maintained
for all objects to refer to.
Circle spot1 = new Circle(2); radius 2
Circle spot2 = new Circle(5); radius 5
Slide 7
PI
PI
3.14
© 2007 Lawrenceville Press
Chapter 8
Instance and Class Methods
 Instance methods must be called from an instance of
the class. Accessor and modifier methods are always
instance methods because they change the state of
an object.
 Class methods are declared using the keyword
static and can be called from the class itself.
Slide 8
© 2007 Lawrenceville Press
Chapter 8
Differences Between Instance and
Class Members
 Instance variables are created each time an object is
declared.
 Class variables are created once for the class and
then objects of the class refer to this copy.
 Instance methods can only be called from an object
of the class.
 Class methods can be called from the class itself.
Slide 9
© 2007 Lawrenceville Press
Chapter 8
The Object Class
 Superclass of all other classes. Classes, such as
Circle and String, are subclasses:
 All subclasses inherit the Object methods, which
include equals() and toString().
 Inherited superclass methods can be redefined, or
overridden, in subclasses.
Slide 10
© 2007 Lawrenceville Press
Chapter 8
Classes Using Classes
 A class containing a member variable that is a class
data type.
 Demonstrates a has-a relationship. The class "has-a"
class.
Slide 11
© 2007 Lawrenceville Press
Chapter 8
Object-Oriented Development
 Objects are selected to model a program
specification
 Objects are created from new classes or from
existing classes
 Objects send messages to other objects to perform a
task
Slide 12
© 2007 Lawrenceville Press
Chapter 8
Features of Object-Oriented Programming
 Reusability: existing classes can be used over and
over again in different applications, which reduces
development time and decreases the likelihood of
bugs.
 Modular: Components are separately written and
maintained.
Slide 13
© 2007 Lawrenceville Press