Objects and Classes

Download Report

Transcript Objects and Classes

Objects and Classes
•
•
•
•
•
Introduction to Classes
Object Variables and Object References
Instantiating Objects
Using Methods in Objects
Reading for this Lecture: L&L, 3.1 - 3.2
1
Introduction to Classes
• A class defines the attributes and behavior
of a specific type of object
– Attributes are variables declared in the class
– Behaviors are methods defined in the class
• Normally, we access an object by calling a
method defined by its class
• We may sometimes access an attribute
defined by its class, but this is discouraged
2
Introduction to Classes
• A class has a name that we can use as if it were
a data type when declaring a variable
• When we declare a variable with the name of a
class as its type, we are creating an object
variable (can contain a reference to an object)
• We access an object’s methods / attributes using
the object variable name and the . notation, e.g.
ClassName objectName;
//object variable
objectName.methodName() // Note: The ( )
objectName.variableName // Note: No ( )
3
Example of a Class Definition
• We can draw a diagram of a class to outline
its important features before writing code –
its name, attributes, and behaviors
Class Name
StateMachine
List of its
Variables
- state :int
...
List of its
Methods
+ setState (int input): void
...
4
Example of a Class Definition
public class StateMachine {
// an attribute or variable
private int state;
// a behavior or method
public void setState (int input) {
state = input;
}
}
5
Example of a Class Definition
• Declaring a StateMachine object variable:
StateMachine myStateMachine = … ;
• Accessing a myStateMachine method:
myStateMachine.setState(1);
• Why can’t we just do this?
myStateMachine.state = 1;
• Notice the word private in the declaration
of the variable state?
6
Prototype for a Class Definition
• We use the Java reserved word private to
prevent access to a variable or method from
code that is written outside the class
• We use the Java reserved word public to
allow access to a variable or method from
code that is written outside the class
• Normally, we declare variables to be private
• Normally, we declare methods to be public
• We will see some valid exceptions later
7
Creating Objects, e.g. String class
• “String” is a commonly used class
• To declare a variable as an object reference,
to a String, we use the class name of the
object as the type name
String title;
• This declaration does not create an object
• It only creates a variable that can hold a
reference to a String object.
8
Creating Objects
• We use the new operator to create an object
title = new String (“Moby Dick");
This calls the String constructor, which is
a special method that sets up the object
• Creating an object is called instantiation
• An object is an instance of a particular class
• title is assigned a reference to a String object
that contains (encapsulates) the string “Moby Dick”
9
Invoking Methods
• Once an object has been instantiated, we
can use the dot operator to invoke or “call”
any of the object’s methods
int count = title.length();
• A method may return a value which can be
used in an assignment or expression
The value of count will be set to 9
• A method invocation can be thought of as
asking an object to perform a service
10